搜索
首页Javajava教程关于Java里面File类创建txt文件重复如何解决

private JButton getOpenButton() {
    if (openButton == null) {
    openButton = new JButton();
    openButton.setText("写入文件");
    // 修改按钮的提示信息openButton.addActionListener(new java.awt.event.ActionListener() {
    // 按钮的单击事件public void actionPerformed(ActionEvent e) {
    // 创建文件对象File file = new File("word.txt");
    try {
    // 创建FileWriter对象FileWriter out = new FileWriter(file);
    // 获取文本域中文本String s = jTextArea.getText();
    out.write(s);
    // 将信息写入磁盘文件out.close();
    // 将流关闭
}
catch (Exception e1) {
    e1.printStackTrace();
}
}
}
);
}
return openButton;
}
private JButton getCloseButton() {
    if (closeButton == null) {
    closeButton = new JButton();
    closeButton.setText("读取文件");
    // 修改按钮的提示信息closeButton.addActionListener(new java.awt.event.ActionListener() {
    // 按钮的单击事件public void actionPerformed(ActionEvent e) {
    File file = new File("word.txt");
    // 创建文件对象try {
    // 创建FileReader对象FileReader in = new FileReader(file);
    char byt[] = new char[1024];
    // 创建char型数组int len = in.read(byt);
    // 将字节读入数组// 设置文本域的显示信息jTextArea.setText(new String(byt, 0, len));
    in.close();
    // 关闭流
}
catch (Exception e1) {
    e1.printStackTrace();
}
}
}
);
}
return closeButton;
}

  如上程序段,刚开始我都认为两个按键都重新创建了woed.txt文件,那么不是覆盖了吗?

  实际上不是的,File类创建word.txt文件并不是真的创建,真要创建,要用file.creatNewfile()才行,实际上两个地方都new File("word.txt"),只是在磁盘内暂时创建了缓存而已

而且因为第一个按键已经创建了,第二个就直接用它(名称一样)。

以上是关于Java里面File类创建txt文件重复如何解决的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版