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("文件打開錯誤");
??}
?}
為啥會出現(xiàn)這種錯誤
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)
2017-02-09
while ?應該是不等于-1啊