java写入文件错误:java.io.FileNotFoundException:文件路径无效
我有一个关于在eclipse中为当前项目编写csv文件的问题java写入文件错误:java.io.FileNotFoundException:文件路径无效,java,file-io,fileoutputstream,Java,File Io,Fileoutputstream,我有一个关于在eclipse中为当前项目编写csv文件的问题 public static void Write_Result(String Amount_Time_Dalta) throws IOException{ File file; FileOutputStream fop = null; String content = ""; String All_Result[] = Amount_Time_Dalta.split("-"); St
public static void Write_Result(String Amount_Time_Dalta) throws IOException{
File file;
FileOutputStream fop = null;
String content = "";
String All_Result[] = Amount_Time_Dalta.split("-");
String path ="/Users/Myname/Documents/workspace/ProjectHelper/"+All_Result[1] + ".csv";
System.out.println(path);
content = All_Result[3]+ "," + All_Result[5] + "\n";
System.out.println(content);
file = new File(path);
fop = new FileOutputStream(file);
file.getParentFile();
if (!file.exists()) {
file.createNewFile();
}
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
}
我得到的错误是
Exception in thread "main" java.io.FileNotFoundException: Invalid file path
at java.io.FileOutputStream.<init>(FileOutputStream.java:215)
at java.io.FileOutputStream.<init>(FileOutputStream.java:171)
at FileDistributor.Write_Result(FileDistributor.java:59)
at FileDistributor.main(FileDistributor.java:29)
读取文件的路径。我工作得很好。
但是,当我使用相同的路径将结果写入文件时(可以存在也可以不存在。我创建或覆盖了一个文件)。它返回无效的文件路径。。。。我真的不知道为什么
更新
刚发现有趣的事。当我只使用File newTextFile=newfile(“1000.csv”);然后它就开始工作了。但是,当我替换为File newTextFile=newfile(filename+“.csv”);它不起作用
这里有一个有效的路径,可以从中创建文件对象:
/Users/Myname/Documents/workspace/ProjectHelper/
但是如果你再看一遍,你会发现它引用的是一个目录,而不是一个可写文件。你的文件名是什么
您的System.out.println
说的是所有结果[1]
的值是多少
示例代码:
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
public class Test
{
public static void main(String[] args)
{
String[] array = {"1000.csv", "800.csv", "700.csv"};
File file;
FileOutputStream fop;
// Uncomment these two lines
//String path = "c:\\" + array[0];
//file = new File(path);
// And comment these next two lines, and the code still works
String path = "c:\\";
file = new File (path + array[0]);
// Sanity check
System.out.println(path);
try
{
fop = new FileOutputStream(file);
}
catch(IOException e)
{
System.out.println("IOException opening output stream");
e.printStackTrace();
}
if (!file.exists())
{
try
{
file.createNewFile();
}
catch(IOException e)
{
System.out.println("IOException opening creating new file");
e.printStackTrace();
}
}
}
}
要使此代码中断,请不要将数组[0]作为文件名传递,只需传入一个空字符串
“
”,您就可以重现错误。我遇到了同样的问题,正在寻找答案。我尝试使用string.trim()并将其放入outputstream,结果成功了。我猜文件路径周围有一些尾随字符或位请查看这篇文章:MarsAtomic//it is string type array。它将是一个文件名。它将类似于1000.csv fileSure,但我不是问“文件名应该是什么?”而是问您文件名实际上是什么。当您将所有结果[1]附加到路径时,您可能会认为您得到的是“path/1000.csv”,但实际上您得到的是“path/”,因为所有结果[1]都是空的。您甚至有一个System.out.println来再次检查,那么实际输出是什么呢?它将是1000.csv…800.csv。我刚发现一件有趣的事。当我只使用File newTextFile=newfile(“1000.csv”);然后它就开始工作了。但是,当我替换为File newTextFile=newfile(filename+“.csv”);它不起作用。再说一次,我不是问文件名是什么。我在问你运行System.out.println时是什么。只要运行代码,告诉我System.out.println是怎么说的。复制并粘贴输出,句号。抱歉,误读了代码中的某些内容。完成path变量的构建后,立即在调试器中设置断点,包括原始路径和扩展名为的文件名。然后看路径在那个点上的值,告诉我它到底说了什么。我几乎可以肯定它没有说你认为它应该说的话。
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
public class Test
{
public static void main(String[] args)
{
String[] array = {"1000.csv", "800.csv", "700.csv"};
File file;
FileOutputStream fop;
// Uncomment these two lines
//String path = "c:\\" + array[0];
//file = new File(path);
// And comment these next two lines, and the code still works
String path = "c:\\";
file = new File (path + array[0]);
// Sanity check
System.out.println(path);
try
{
fop = new FileOutputStream(file);
}
catch(IOException e)
{
System.out.println("IOException opening output stream");
e.printStackTrace();
}
if (!file.exists())
{
try
{
file.createNewFile();
}
catch(IOException e)
{
System.out.println("IOException opening creating new file");
e.printStackTrace();
}
}
}
}