Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java txt文件保存到文件夹_Java_File_Text_Save_Writefile - Fatal编程技术网

将Java txt文件保存到文件夹

将Java txt文件保存到文件夹,java,file,text,save,writefile,Java,File,Text,Save,Writefile,首先,我爱你们所有人!每个人都很乐于助人!可悲的是,当我去回答问题时,他们对我来说都太超前了:'( 我想将文本文件保存到一个文件夹中,但不是绝对文件夹,例如,我想将其保存到一个文件夹中 {class location}/text/out.txt 因为程序在不同的计算机上运行,所以位置发生了变化,所以我不能把C://ect放进去 我也知道我需要使用怀疑“\\”——但这在我的尝试中没有起作用 public void writeFile (int ID, int n) { tr

首先,我爱你们所有人!每个人都很乐于助人!可悲的是,当我去回答问题时,他们对我来说都太超前了:'(

我想将文本文件保存到一个文件夹中,但不是绝对文件夹,例如,我想将其保存到一个文件夹中

{class location}/text/out.txt

因为程序在不同的计算机上运行,所以位置发生了变化,所以我不能把C://ect放进去

我也知道我需要使用怀疑“\\”——但这在我的尝试中没有起作用

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

您可以使用getAbsolutePath()函数:

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

我建议您看看线程,您应该首先创建目录,然后创建文件。记住首先检查它们是否存在:

new File("some.file").exists();
new File("folder").mkdir(); // creates a directory
new File("folder" + File.separator + "out.txt"); // creates a file
如果资源已经存在,则不需要创建
文件
对象


File.separator
是斜杠本地化问题的答案。

在代码目录中创建名为text的文件夹是独立于文件系统的。要在
{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();
     }

不要忘记捕获
IOException

将.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
然后,您可以随时调用此方法,它会将要写入.txt文档的所有代码保存到项目文件夹的根目录中

然后,您可以在任何计算机上运行应用程序,它仍然会保存文档以供在任何计算机上查看。

超级棒的东西!谢谢!!