java进程:找不到文件
在执行以下代码时,它在a:28(注释中)处给出了一个文件未找到错误。这是因为目录未刷新,还是子进程在执行a:28行之前未创建文件java进程:找不到文件,java,file,Java,File,在执行以下代码时,它在a:28(注释中)处给出了一个文件未找到错误。这是因为目录未刷新,还是子进程在执行a:28行之前未创建文件 File outputFile = new File("RunstatsCmd.db2"); FileWriter out = new FileWriter(outputFile); String cmd = "DB2CMD;DB2;"+" EXPORT TO "+ "\"C:\\file.xml\"" +" OF IXF MESSAGES "+"\"C:\\msg
File outputFile = new File("RunstatsCmd.db2");
FileWriter out = new FileWriter(outputFile);
String cmd = "DB2CMD;DB2;"+" EXPORT TO "+ "\"C:\\file.xml\"" +" OF IXF MESSAGES "+"\"C:\\msg.txt\""+" SELECT * FROM OLTP.ACCOUNT_DETAILS";
out.write("CONNECT TO OLTPDB;\n");
out.write(cmd + ";\n");
out.write("CONNECT RESET;\n");
out.close();
System.out.println("before creating connection....");
Class.forName ("com.ibm.db2.jcc.DB2Driver").newInstance ();
Process p = Runtime.getRuntime().exec("db2 -vtf RunstatsCmd.db2");
// open streams for the process's input and error
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
String s;
// read the output from the command and set the output variable with
// the value
while ((s = stdInput.readLine()) != null)
{
System.out.println(s);
}
// read any errors from the attempted command and set the error
// variable with the value
while ((s = stdError.readLine()) != null)
{
System.out.println(s);
}
// destroy the process created
// delete the temporary file created
outputFile.deleteOnExit();
System.out.println("query executed...");
p.waitFor();
//a:28////////////////////////
FileInputStream fis=new FileInputStream("C:\\file.xml");
int i;
while((i=fis.read())!=-1){
System.out.println((char)i);
}
}catch(Exception e){
e.printStackTrace();
}
在java字符串中,
\
是一个转义字符,因此如果您想按字面意思使用它,就必须对其进行转义
因此,要表示C:\file.xml,您需要使用以下java字符串:“C:\\file.xml”
但请注意,在java中,即使在windows中,也可以始终使用前向斜杠作为路径分隔符,因此这也应该有效:
FileInputStream fis = new FileInputStream("C:/file.xml");
字符串中的反斜杠需要转义
"C:\\file.xml"
或者,使用前斜杠(Java中也接受,甚至在Windows机器上也接受)
当用于
文件路径时,请小心使用\
字符
将代码更改为:
FileInputStream fis=new FileInputStream("C:\\file.xml");
\
是转义字符
或者,您可以使用:
FileInputStream fis=new FileInputStream("C:/file.xml");
有趣的是,他在上面做,但不是在下面。我想即使你意识到需要,也很容易错过。特别是当你碰巧遇到一个有效的转义序列,如\f
时,编译器不会提醒你。除了你的文件问题,你的exec()
也不会工作-请参阅:
FileInputStream fis=new FileInputStream("C:/file.xml");