zoukankan      html  css  js  c++  java
  • Java 文本文件 读写

    Use File/FileInputStream/FileOutputStream.

    public void testWithFIS() throws IOException{
            File file=new File("Test.txt");
            FileInputStream fis=new FileInputStream(file);
            
            System.out.println("total file size:"+fis.available());
            
            int content;
            while((content = fis.read()) != -1){
                System.out.println((char)content);
            }
             
            fis.close();
             
        }
         
        public void testWithFISWrite() throws IOException{
            File file=new File("Test.txt");
            FileOutputStream fos=new FileOutputStream(file);
            
            
            for (int i = 0; i < 5; i++) {
                fos.write((int)'x');
            }
            
            fos.close();
             
        }
        

    FileReader/BufferedReader

    FileWriter/BufferedWriter

        public void testWithBufferReader() throws IOException{
            FileReader file=new FileReader("Test.txt");
            BufferedReader br=new BufferedReader(file);
            
            String currentLine;
            while((currentLine = br.readLine()) != null){
                System.out.println(currentLine);
            }
            
            br.close();
            file.close();
        }
        
        @Test
        public void testWithBufferWriter() throws IOException{
            FileWriter file=new FileWriter("Test.txt");
            BufferedWriter writer= new BufferedWriter(file);
            
            String currentLine="hello, this is ross";
            writer.write(currentLine);
            writer.newLine();
            writer.write("second line");
            writer.close();
            file.close();
        }
  • 相关阅读:
    从官方下载 Bootstrap 版本 并写 第一个页面
    南阳477
    南阳463
    南阳455
    南阳399
    南阳276
    南阳275
    南阳268
    南阳264
    南阳263
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/6245269.html
Copyright © 2011-2022 走看看