FileTest

CreatFile

 1 package filetest;
 2 import java.io.*;
 3 import java.util.*;
 4 public class CreatFile {
 5 
 6     public CreatFile(){
 7             File file = new File("E:\eclipse\filetest"+File.separator+"hello.txt");
 8         try {
 9             file.createNewFile();
10         } catch (IOException e) {
11             // TODO Auto-generated catch block
12             e.printStackTrace();
13             }
14        Scanner fin;
15     try {
16         fin = new Scanner(file);
17         while(fin.hasNext()){
18            String s = fin.next();
19            System.out.println(s);
20            
21        }
22     } catch (FileNotFoundException e) {
23         // TODO Auto-generated catch block
24         e.printStackTrace();
25     }
26        
27     }
28     public static void main(String[] args) throws IOException {
29     //    CreatFile f = new CreatFile();
30         File file = new File("E:\eclipse\filetest"+File.separator+"hello.txt");
31         file.createNewFile();
32         Scanner fin = new Scanner(file);
33         while(fin.hasNext()){
34            String s = fin.next();
35            System.out.println(s);
36            
37        }
38 
39     }
40 
41 }

InputStream

 1 package filetest;
 2 
 3 import java.io.*;
 4 
 5 
 6 public class InPutStream {
 7 
 8     public static void main(String[] args) throws IOException {
 9      int len;
10      byte[] buffer = new byte[100];
11      File file = new File("E:\eclipse\filetest"+File.separator+"hello_1.txt");
12      FileInputStream fin = new FileInputStream(file);
13      while((len=fin.read(buffer))>0){
14         String s = new String(buffer,0,len);
15         System.out.println(s);
16          
17      }
18     }
19 
20 }

OutputStream

 1 package filetest;
 2 import java.io.*;
 3 public class OutPutStream {
 4 
 5     public static void main(String[] args) throws IOException {
 6     byte[] buffer = "get string!".getBytes();
 7     String filename = "E:\eclipse\filetest"+File.separator+"hello_1.txt";
 8     FileOutputStream fout = new FileOutputStream(filename);
 9     
10     fout.write(buffer);
11 
12     //System.out.println();
13     }
14 
15 }

ReaderWriter

 1 package filetest;
 2 import java.io.*;
 3 
 4 public class ReaderWriter {
 5 
 6     public static void main(String[] args) throws IOException {
 7         //读入字符数据
 8     char s[] = new char [10];
 9     File file = new File("E:\eclipse\filetest"+File.separator+"hello.txt");
10     FileReader fin = new FileReader(file);
11     fin.read(s);
12     System.out.println(s);
13     fin.close();
14         //输出字符数据
15     char []c ={'j','a','v','a'};
16    //File file = new File("E:\eclipse\filetest"+File.separator+"hello.txt");
17     FileWriter fout = new FileWriter(file);
18     fout.write(c);
19     System.out.println(c);
20     fout.close();
21     }
22 
23 }
原文地址:https://www.cnblogs.com/the-wang/p/6753132.html