Javaバイトストリーム&文字ストリームの使用方法

3086 ワード

  • Fileの作成とその基本的な使用方法
  • public class FileTest {
        public static void main(String[] args) throws IOException {
            java.io.File f = new java.io.File("D:\\demo.txt");
            System.out.println("     :" +f);
            //      
            System.out.println("      :"+f.exists());
    
            //      
            System.out.println("        :"+f.isDirectory());
    
            //     (    )
            System.out.println("       :"+f.isFile());
    
            //    
            System.out.println("       :"+f.length());
    
            //        
            long time = f.lastModified();
            Date d = new Date(time);
            System.out.println("           :"+d);
            //         1970.1.1 08:00:00
            f.setLastModified(0);
    
            //     
            File f2 =new File("d:/test.txt");
            f.renameTo(f2);
            System.out.println("Rename successfully~");
    
        }
    }
    
    
  • 入力バイトストリーム
  • public class FileInputStreamTest {
        public static void main(String[] args) {
            File f = new File("D:\\test.txt");
            try(FileInputStream fis = new FileInputStream(f);) {
                byte[] all = new byte[(int)f.length()];
                fis.read(all);
                for (var tt:all) {
                    System.out.println(tt);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 出力バイトストリーム
  • public class FileOutputStreamTest {
        public static void main(String[] args) {
            File f = new File("D:\\Demo.txt");
            try {
                FileOutputStream fos = new FileOutputStream(f);
                byte[] data = {97, 98};
                fos.write(data);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 入力文字ストリーム
  • public class FileReaderTest {
        public static void main(String[] args) {
            File f = new File("D:\\test.txt");
            try (FileReader fr = new FileReader(f)) {
                //       ,          
                char[] all = new char[(int) f.length()];
                //                
                fr.read(all);
                for (char b : all) {
                    //      A B C
                    System.out.println(b);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    
    
  • 出力文字ストリーム
  • public class FileWriterTest {
        public static void main(String[] args) {
            File f = new File("D:\\test2.txt");
            try(FileWriter fr = new FileWriter(f)) {
                //                 
                String data="abcde456agawag890";
                char[] cs = data.toCharArray();
                fr.write(cs);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    

    **
    まとめ
    文字ストリームとバイトストリームの違い
                 ;           Unicode  。
               ;        (JVM  )。
                  ,    、  、     
               ,        Unicode  ,     
    

    **