• 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();
        }
  • 相关阅读:
    day5
    \_\_setitem\_\_和\_\_getitem和\_\_delitem__
    描述符(\_\_get\_\_和\_\_set\_\_和\_\_delete\_\_)
    \_\_getattribute\_\_
    面向对象进阶小结
    property装饰器
    super()方法详解
    菱形继承问题
    类的派生
    类的继承
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/6245269.html
Copyright © 2020-2023  润新知