使用Java读写文件
File file = new File("./test.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
try {
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(out);
writer.write("hello, world!");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[10];
in.read(bytes);
System.out.println(new String(bytes));
} catch (Exception e) {
e.printStackTrace();
}
最后更新于1年前
本文由人工编写,AI优化,转载请注明原文地址: 使用Java读写文件
推荐阅读
评论 (0)
发表评论
昵称:加载中...
暂无评论,快来发表第一条评论吧!