• 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 }
  • 相关阅读:
    20155302 《信息安全系统设计基础》课程总结
    2017-2018-1 20155302 第十四周作业
    2017-2018-1 20155302 第十三周作业
    2017-2018-1 20155302 实验五 通信协议设计
    20155302实验一
    2017-2018-1 20155302 实验四 外设驱动程序设计
    2017-2018-1 20155302 《信息安全系统设计基础》第11周学习总结
    # 2017-2018-1 20155302 课下实践IPC及课上补充
    2017-2018-1 20155302 实验三 实时系统
    # 2017-2018-1 20155302 《信息安全系统设计基础》第9周学习总结
  • 原文地址:https://www.cnblogs.com/the-wang/p/6753132.html
Copyright © 2020-2023  润新知