问答详情
源自:2-2 遍历目录

FileOutputStream

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ReadFileData {
 public static void main(String ags[]){
  int b;
  Label lab;
  TextArea textArea;
  byte temp[]=new byte[100];
  Frame window=new Frame();window.setSize(400,400);
  lab=new Label();
  lab.setText("文件d:/write.txt的内容如下:");textArea=new TextArea(10,16);
  window.add(lab,BorderLayout.NORTH);window.add(textArea,BorderLayout.CENTER);
  window.validate();window.setVisible(true);
  window.addWindowListener(new WindowAdapter()
    {
   public  void windowClosing(WindowEvent e){
    System.exit(0);
   }
    });
  try{
   File f=new File("D:/迅雷下载/test.txt");
   FileInputStream readfile=new FileInputStream(f);
   while((b=readfile.read(temp,0,100))!=1){
    String s=new String(temp,0,b);
    textArea.append(s);
   }
   readfile.close();
  }
  catch(IOException e){
   lab.setText("文件打开错误");
  }
 }

为啥会出现这种错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
 at java.lang.String.checkBounds(Unknown Source)
 at java.lang.String.<init>(Unknown Source)
 at Test.ReadFileData.main(ReadFileData.java:29)

提问者:浮世之绘0 2017-02-09 11:36

个回答

  • 慕码人0628686
    2017-02-09 12:07:34
    已采纳

    while  应该是不等于-1啊