java进程:找不到文件

时间:2012-07-20 18:01:58

标签: 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.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();
}

3 个答案:

答案 0 :(得分:1)

字符串中的反斜杠需要转义。

"C:\\file.xml"

或者,使用正斜杠(它们在Java中被接受,即使在Windows机器上也是如此)。

"C:/file.xml"

答案 1 :(得分:0)

在java字符串中,\是一个转义字符,因此如果你想按字面意思使用它,必须对其进行转义。

因此,要表示 C:\ file.xml ,您需要使用此java字符串:"C:\\file.xml"

但请注意,在java中,你总是可以使用正斜杠作为路径分隔符 - 即使在windows中 - 所以这也应该有效:

FileInputStream fis = new FileInputStream("C:/file.xml");

答案 2 :(得分:0)

使用\时,请谨慎使用file path字符。

将您的代码更改为:

FileInputStream fis=new FileInputStream("C:\\file.xml");

\是转义字符。

或者您也可以使用:

FileInputStream fis=new FileInputStream("C:/file.xml");