Javaバイトストリーム&文字ストリームの使用方法
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 ,
**