java.lang.String.getBytes()でファイルを書き込むサンプル

2018年3月11日

Javaコード
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BytesFileDemo {

public static void main(String[] args) {
String fileName = “C:\\arkgame" + File.separator + “hello.txt";
File f = new File(fileName);

try {
OutputStream out = new FileOutputStream(f);

String str = “this is a test";
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}

}

Java

Posted by arkgame