我正在执行一个命令,它返回一个文件的修订号; '文件名'.但是如果执行命令时出现问题,则应用程序会挂起.我该怎么做才能避免这种情况?请在下面找到我的代码.
String cmd= "cmd /C si viewhistory --fields=revision --project="+fileName;
Process p = Runtime.getRuntime().exec(cmd) ;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我想以追加模式写入临时文件.我看到文件已创建,但Stringbuffer中的数据未写入.有人可以告诉我为什么吗?请在下面找到我写的代码,
public static void writeToFile(String pFilename, StringBuffer sb)
throws IOException {
String property = "java.io.tmpdir";
String tempDir = System.getProperty(property);
File dir = new File(tempDir);
File filename = File.createTempFile(pFilename, ".tmp", dir);
FileWriter fileWriter = new FileWriter(filename.getName(), true);
System.out.println(filename.getName());
BufferedWriter bw = new BufferedWriter(fileWriter);
bw.write(sb.toString());
bw.close();
}
Run Code Online (Sandbox Code Playgroud) 我需要从 eclipse 的属性文件中读取配置详细信息。我已将其放在与 我调用的类文件config.properties相同的级别plugin.xml中:
Properties properties = new Properties();
FileInputStream file;
String path = "./config.properties";
file = new FileInputStream(path);
properties.load(file);
Run Code Online (Sandbox Code Playgroud)
我得到一个file not found exception. 有没有更好的方法来做到这一点?
有人可以帮我修复我的问题.我有一个函数,它检查特定路径中是否存在文件.该函数检查文件名是否匹配,路径是否也匹配.(具有特定名称的文件可能存在于多个位置).请在下面找到我的代码.
memberPath是一个包含相对路径的静态变量.file_Path是一个静态变量,在找到匹配项后会更新.
我的问题是函数找到了匹配,但它突然出现for循环返回语句但返回到for循环.有人可以帮我修改我的代码,这样一旦找到匹配,它就会将bac返回到调用位置.
public static String traverse(String path, String filename) {
String filePath = null;
File root = new File(path);
File[] list = root.listFiles();
for (File f : list) {
if (f.isDirectory()) {
traverse(f.getAbsolutePath(), filename);
} else if (f.getName().equalsIgnoreCase(filename) && f.getAbsolutePath().endsWith(memberPath)) {
filePath = f.getAbsolutePath();
file_Path = filePath;
break ;
}
}
return filePath;
}
Run Code Online (Sandbox Code Playgroud)