简体   繁体   English

将Java txt文件保存到文件夹

[英]Save Java txt file to folder

First of all - I love you all at Stackoverflow! 首先-我在Stackoverflow上爱你们所有人! Everyone is very helpful! 每个人都非常有帮助! Sadly when I go to answer questions they are all too advance for me :'( 可悲的是,当我去回答问题时,它们对我来说太先进了:'(

I want to save the text file to a folder - but not an absolute folder for example I want to save it to 我想将文本文件保存到文件夹-但不是绝对文件夹,例如,我要将其保存到

{class location}/text/out.txt {课程位置} /text/out.txt

Because the program is being worked on different computers the location changes, so I can't put C:// ect 由于该程序在不同的计算机上运行,​​因此位置会发生变化,因此我无法将C://放在

I also know I need to use doubt "\\\\" - but this didn't work in my attempts 我也知道我需要使用疑问“ \\\\”-但这在我的尝试中不起作用

public void writeFile (int ID, int n) {
            try{
                    String Number = Integer.toString(n);
                    String CID = Integer.toString(ID);
          FileWriter fstream = new FileWriter("//folder//out.txt",true); //this don't work 
          BufferedWriter out = new BufferedWriter(fstream);
          out.write(Number+"\t"+CID);
          out.newLine();
          out.close();
          }//catch statements etc

you can use getAbsolutePath() function: 您可以使用getAbsolutePath()函数:

 FileWriter fstream = new FileWriter(new File(".").getAbsolutePath()+"//folder//out.txt",true);

and i sugest you to take a look at this thread 我建议你看看这个话题

Creating a folder named text in the code's directory is file system independent. 在代码目录中创建名为text的文件夹与文件系统无关。 To create a file in {project folder}/text/out.txt you can try this: 要在{project folder}/text/out.txt创建文件,您可以尝试以下操作:

String savePath = System.getProperty("user.dir") + System.getProperty("file.separator") + text;
File saveLocation = new File(savePath);
    if(!saveLocation.exists()){
         saveLocation.mkdir();
         File myFile = new File(savePath, "out.txt");
         PrintWriter textFileWriter = new PrintWriter(new FileWriter(myFile));
         textFileWriter.write("Hello Java");
         textFileWriter.close();
     }

Don't forget to catch the IOException ! 不要忘记捕捉IOException

The simplest way to make it save your .txt to the root of your folder is to do this: 使其保存.txt到文件夹根目录的最简单方法是:

public class MyClass 
{
public void yourMethod () throws IOException 
{

FileWriter fw = null;

try

{

 fw = new FileWriter ("yourTxtdocumentName.txt");

// whatever you want written into your .txt document

fw.write ("Something");
fw.write ("Something else");
System.out.println("Document completed.");
fw.close

}
catch (IOException e)
{
e.printStackTrace();
}

} // end code
} // end class

Then you call this method whenever you like, it will save whatever you have coded to be written into the .txt document into the root of your project folder. 然后,您随时可以调用此方法,它将把您编写的任何代码保存到项目文件夹的根目录中。

You can then run your application on any computer, and it will still save the document for viewing on any computer. 然后,您可以在任何计算机上运行您的应用程序,它将仍然保存文档以在任何计算机上查看。

You should first create directories and then the files. 您应该首先创建目录,然后创建文件。 Remember to firstly check their existence: 请记住首先检查它们的存在:

new File("some.file").exists();
new File("folder").mkdir(); // creates a directory
new File("folder" + File.separator + "out.txt"); // creates a file

Don't need to create a File object if resource already exist. 如果资源已经存在,则不需要创建File对象。

File.separator is the answer for your localization problems with slashes. File.separator是您使用斜杠解决本地化问题的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM