码迷,mamicode.com
首页 > 其他好文 > 详细

FileTest

时间:2017-04-23 17:58:52      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:eclips   exce   ext   txt   fileread   ted   let   sys   int   

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 }

 

FileTest

标签:eclips   exce   ext   txt   fileread   ted   let   sys   int   

原文地址:http://www.cnblogs.com/the-wang/p/6753132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!