单个读取
package IO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class IoTest {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\新建文本文档.txt");
try {
int s1 = fileInputStream.read();
System.out.println(s1);
int s2 = fileInputStream.read();
System.out.println(s2);
int s3 = fileInputStream.read();
System.out.println(s3);
int s4 = fileInputStream.read();
System.out.println(s4);
int s5 = fileInputStream.read();
System.out.println(s5);
int s6 = fileInputStream.read();
System.out.println(s6);
int s7 = fileInputStream.read();
System.out.println(s7);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(fileInputStream!=null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
循环处理
package IO;
import java.io.FileInputStream;
public class IoWhileTest {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("C:\\\\Users\\\\Administrator\\\\Desktop\\\\新建文本文档.txt");
int temp = 0;
StringBuilder stringBuilder = new StringBuilder();
while ((temp = fileInputStream.read()) != -1) {
System.out.println(temp);
stringBuilder.append((char) temp);
}
System.out.println(stringBuilder);
} catch (Exception e) {
}
}
}