【问题标题】:Fileoutputstream in eclipse. relative pathEclipse 中的文件输出流。相对路径
【发布时间】:2017-11-04 17:07:38
【问题描述】:

有没有办法在 eclipse 中保存 .txt 文件?

目前我在做:

FileOutputStream fout = new FileOutputStream("C:\\Users\\xxxx\\mytxtfile.txt", false);

我希望它位于项目的 eclipse 文件夹中,而不是绝对路径。

【问题讨论】:

  • .\\mytxtfile.txt?但也许你应该考虑使用“资源”
  • 仅使用相对路径保存应将其放置在 Eclipse 文件夹中的某个位置。你试过这样做吗?
  • FileOutputStream fout = new FileOutputStream("mytxtfile.txt", false); 应该在项目的根目录下创建文件。

标签: java eclipse fileoutputstream


【解决方案1】:

试试这个

FileOutputStream fout = new FileOutputStream("mytxtfile.txt", false);

如果您想使用项目根目录中的文件夹,我建议这样做:

File root = new File("yourfolder");
root.mkdir(); //this makes sure the folder exists
File file = new File(root,"mytextfile.txt");
FileOutputStream fout = new FileOutputStream(file, false);

获取 eclipse 3.3 的 eclipse 安装位置(我不知道你为什么要这样做,但仍然)System.getProperty("eclipse.home.location");

对于较新的 Eclipse 版本,我真的不知道。

【讨论】:

  • 它不在我的项目中,而是在我的eclipse安装的地方。
  • @Joe 我帮不了你,但我建议你编辑你的问题以明确这一点。
【解决方案2】:

您可以在 Eclipse 应用程序启动配置中设置所需的任何初始文件夹。默认情况下它是项目根目录,但这并不理想;最好是在项目中使用一些文件夹,如“运行”,您可以将其添加到 .gitignore。

然后空路径 (new File("")) 会解析到该文件夹​​,如果需要,您还可以指定子路径。

【讨论】:

    【解决方案3】:

    尝试以下方法:

    PdfWriter.getInstance(document, newFileOutputStream("C:/Users/xxxx/mytxtfile.txt",false));
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2020-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多