-1

在执行以下代码时,它会在 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();
}
4

3 回答 3

1

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

"C:\\file.xml"

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

"C:/file.xml"
于 2012-07-20T18:06:06.317 回答
0

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

所以要表示C:\file.xml,你需要使用这个 java String: "C:\\file.xml"

但请注意,在 java 中,您始终可以使用正斜杠作为路径分隔符 - 即使在 Windows 中也是如此 - 所以这也应该有效:

FileInputStream fis = new FileInputStream("C:/file.xml");
于 2012-07-20T18:05:36.240 回答
0

使用 for 时请\谨慎使用字符file path

将您的代码更改为:

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

\是一个转义字符。

或者,您可以使用:

FileInputStream fis=new FileInputStream("C:/file.xml");
于 2012-07-20T18:09:07.080 回答