JAVA InputStream读取文件操作
package IO;
import java.io.*;
public class IOtest01 {
public static void main(String[] args) {
File src=new File("src/IO/abc.txt");
try {
InputStream is=new FileInputStream(src);
int data1=is.read();
int data2=is.read();
int data3=is.read();
is.close();
System.out.println((char)data1);
System.out.println((char)data2);
System.out.println((char)data3);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package IO;
import java.io.*;
public class IOtest02 {
public static void main(String[] args) {
File src=new File("src/IO/abc.txt");
InputStream is=null;
try {
is=new FileInputStream(src);
byte[] flash=new byte[1024*10];
int len=-1;
while((len= is.read(flash))!=-1){
String str=new String(flash,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null!=is){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}