import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class fiIenOuputStreamDemo {
public static void main(String[] args) {
//读文件从文件(或键盘,网络)
try {//所有**非RuntimeException类及其子类**的编译时异常都要抓异常
//E:\\a.txt是文件的位置,java中"\\"或"/"代表"\"
FileInputStream in=new FileInputStream("E:/a.txt");//new一个对象代表E:\\a.txt文件,对in的操作,就是对文件的操作
int v=-1;
while((v=in.read())!=-1){//读文件
System.out.print((char)v);
}
in.close();//关闭文件
} catch (IOException e) {
e.printStackTrace();
}
//写文件到文件(或显示器,打印机,网络)
String str="sfdghdgh2653";
try {
FileOutputStream out=new FileOutputStream("E:\\b.txt");
byte buf[]=str.getBytes();//返回str数组
out.write(buf);//把数组buf写入文件E:\\b.txt
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ACM中的scanner类也可以读文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class scannerDemo {
public static void main(String[] args) {
Scanner sc;
try {
sc = new Scanner(new FileInputStream("G:\\a.txt"));
while(sc.hasNext()){
String str=sc.next();
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}