「java」テキストファイルを読み込むサンプルコード
Javaコード
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ReadWriteDeno {
public void writeFile() {
String str = “test prpgrame";
try {
FileOutputStream out = new FileOutputStream(“c:/tiantian/ReadWrite2.txt");
out.write(str.getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void readFile() {
try {
FileInputStream in = new FileInputStream(“c:/tiantian/read22.txt");
byte bs[] = new byte[in.available()];
in.read(bs);
System.out.println(“file content=\n" + new String(bs));
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ReadWriteDeno t = new ReadWriteDeno();
t.readFile();
t.writeFile();
}