首先,打开一个模数银txt文件,File file = new File("文件路径");
然后,封装输出流,DataOutputStream os = new DataOutputStream(new FileOutputStream(file));
接着旦宴,往os里面写数据,os.writeInt(...) os.writeByte(...) os.writeChar(...)等等,你要写什么样类型的数毕悔据,就调用什么样类型的方法。
最后,记得关掉输出流,调用os.close()
Java通过使用I/O文件操作类,来创建输入输出流,将数据保存在file tet文件里面。示例如下:
package *###&*_1_*###&*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/newfile.txt"竖扒);
fop = new FileOutputStream(file);
漏州 // if file doesnt exists, then create it
if (!file.exists()) {
返纤蔽 file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
利用file inputstream outputstream 进行数据流的操作
通过文件输出流来实现,