【问题标题】:How to save a file in java如何在java中保存文件
【发布时间】:2014-03-14 17:32:25
【问题描述】:

我正在尝试从日志报告中创建一个文件。为了保存文件,我创建了一个按钮。当按下按钮时,执行以下代码:

public void SAVE_REPORT(KmaxWidget widget){//save
  try {
    String content = report.getProperty("TEXT");
    File file = new File("logKMAX.txt");
    // if file doesnt exists, then create it
    if (!file.exists()) {
      file.createNewFile();
    }

    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();
  } catch (IOException e) {
    e.printStackTrace();
  }
} //SAVE_REPORT

我没有编译错误,但没有保存任何文件。

你知道什么可能是错的吗?

【问题讨论】:

  • 这对我有用。检查是否正在输入方法。另外,请记住该文件将在项目文件夹中生成。
  • 我能想到的两种情况:1-文件保存在不同的位置2-您正在写入的目录是只读的
  • 您仅使用文件名,因此路径(如果与您运行代码的目录相关)。打印file.getAbsoluteFile() 的值并检查文件是否存在。如果不存在,请检查您的写入权限。
  • @Mohammad:在任何地方都找不到该文件。我用locatefind linux 命令搜索,它不存在。在这个目录中我可以创建和删除文件。

标签: java file save


【解决方案1】:

使用新的文件 API。一方面,在你的程序中,你没有验证.createNewFile()的返回值:它不会在失败时抛出异常......

使用新的文件 API,它变得更加简单:

public void saveReport(KmaxWidget widget)
    throws IOException
{
    final String content = report.getProperty("TEXT");
    final Path path = Paths.get("logKMAX.txt");

    try (
        final BufferedWriter writer = Files.newBufferedWriter(path,
            StandardCharsets.UTF_8, StandardOpenOption.CREATE);
    ) {
        writer.write(content);
        writer.flush();
    }
}

【讨论】:

  • 我的编译器似乎无法识别PathPathsStandardCharsetsStandardOpenOptionFiles...我需要导入什么吗?
  • 啊,你还在用Java 6?
  • 不!我用7! thanos@thanos-laptop:~$ java -version java version "1.7.0_51" OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2) OpenJDK Server VM (build 24.45-b08, mixed mode)
  • 好的,但是javac -version 说了什么? (注意:javac,不是 java)
  • @Thanos 如果你想使用FilesPathPathsStandardOpenOption,请将导入添加到java.nio.file.*StandardCharsets 也位于java.nio.charset。如果您使用 Eclipse 之类的 IDE,只需让它为您导入即可。只需使用Ctrl+Shift+O,或按Source -> Organize imports
【解决方案2】:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class moveFolderAndFiles
{
    public static void main(String[] args) throws Exception 
    {   
        File sourceFolder = new File("c:\\Audio Bible");

        copyFolder(sourceFolder);
    }
    private static void copyFolder(File sourceFolder) throws Exception
    {
        File files[] = sourceFolder.listFiles();
        int i = 0;

        for (File file: files){
            if(file.isDirectory()){
                File filter[] = new File(file.getAbsolutePath()).listFiles();
                for (File getIndividuals: filter){
                    System.out.println(i++ +"\t" +getIndividuals.getPath());
                    File des = new File("c:\\audio\\"+getIndividuals.getName());
                    Files.copy(getIndividuals.toPath(), des.toPath(), StandardCopyOption.REPLACE_EXISTING);
                }
            }
        }
    }
}

【讨论】:

  • 虽然这有它自己的弱点。 2个for循环只能处理1级子文件夹。
  • 在回答一个老问题时,如果您包含一些上下文来解释您的答案如何提供帮助,那么您的答案将对其他 StackOverflow 用户更有用,特别是对于已经有一个已接受答案的问题。请参阅:How do I write a good answer
  • 是的,先生。我也注意到了,但这实际上是我的第一篇文章,我摸索了界面。将代码和解释结合起来对我来说很困难。我会好起来的,听从你的建议。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 2014-05-11
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
相关资源
最近更新 更多