源码:
//FileInputStream和FileOutputStream的实例
public static void main(String[] args) {
FileIOTest ft=new FileIOTest();
String path="./file/";
File f=new File(path);
if(!f.exists()){
f.mkdirs();
}
f=new File(path+"test.jpg");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//将控制台的数据记录到文件中
/*String line="";
do{
Scanner scanner=new Scanner(System.in);
System.out.println("输入exit退出!请输入你需要记录的数据:");
line=scanner.nextLine();
FileOutputStream fos=null;
try {
if(!line.equals("exit")){
fos=new FileOutputStream(f,true);
//OutputStream类及其子类操作的是字节byte,所以用byte数组来接收
byte b[]=(line+"\r\n").getBytes();
fos.write(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}while(!line.equals("exit"));*/
//从文件读出,并打印出来
/*FileInputStream fis=null;
try{
fis=new FileInputStream(f);
byte b[]=new byte[1024];
int val=0;
while((val=fis.read(b))!=-1){
System.out.println(new String(b,0,val));
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
//拷贝图片
File imgFile=new File("./images/");
if(!imgFile.exists()){
imgFile.mkdirs();
}
imgFile=new File("./images/copy.jpg");
ft.copyFile(f, imgFile);
}
//拷贝文件
public void copyFile(File input,File output){
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(input);
fos=new FileOutputStream(output);
byte b[]=new byte[1024];
int i=0;
while((i=fis.read(b))!=-1){
fos.write(b, 0, i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fis!=null){
fis.close();
}
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//FileReader和FileWriter类的实例
public static void main(String[] args) {
//FileReader,FileWriter
FileReader fr=null;
FileWriter fw=null;
/*try {
fr=new FileReader("./file/test.txt");
fw=new FileWriter("./images/copy_test.txt");
//FileReader 操作的是字符char,因此只能用char数组接收
char ch[]=new char[1024];
int n=0;
while((n=fr.read(ch))!=-1){
System.out.println(new String(ch,0,n));
fw.write(ch, 0, n);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(fw!=null){
fw.close();
}
if(fr!=null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}*/
//BufferedReader和BufferedWriter实例
BufferedReader br=null;
BufferedWriter bw=null;
try {
fr=new FileReader("./file/test.txt");
br=new BufferedReader(fr);
fw=new FileWriter("./images/copy_test.txt");
bw=new BufferedWriter(fw);
//BufferedReader最大的优势就是直接操作字符串String,它可按行读取,效率最高
String line="";
while((line=br.readLine())!=null){
System.out.println(line);
bw.write(line+"\r\n");//readLine方法不会读取换行符
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(bw!=null){
bw.close();
}
if(br!=null){
br.close();
}
if(fw!=null){
fw.close();
}
if(fr!=null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
javaIO使用方法总结:
1.InputStream,OutputStream类及其子类处理的都是字节,用byte的数组接收,此类将数据记录到byte这个数组中,然后转化这个数组即可。
2.Reader,Writer类及其子类处理的是字符char,用char的数组接收,此类将数据记录在char这个数组上,然后操作这个char数组即可。
3.BufferedReader,BufferedWriter操作的是String,用String直接接收,且提供方便的readLine方法,效率较高。