我有一个方法调用第二个方法,第二个将是:
- 创建任何丢失的目录
- 创建文件
- 将2D字符串[]解码为字符串(无效)
- 写入内容
- 将解码后的字符串写入带有头的文件(无效)
第一种方法
public static boolean store(Exception exception, String[][] flags){
return storePrivate(exception, location, flags);
}
第二种方法(不是所有代码,只是相关代码)
private static boolean storePrivate(Exception exception, String dir, String[][] flags){
String flag = "";
for(int i = 0; i >= flags.length; i++){
flag = flag + "" + flags[i][0] + ": " + flags[i][1] + "n";
}
try {
File directory = new File(dir);
File file = new File(dir + id + ".txt");
if (!directory.exists()) {
directory.mkdirs();
}
file.createNewFile();
FileWriter filewriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter writer = new BufferedWriter(filewriter);
if(flag != ""){
writer.write("Flags by Developer: ");
writer.write(flag);
}
writer.flush();
writer.close();
return true;
} catch (IOException e) {
return false;
}
}
调用第一个方法
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
// TODO Auto-generated catch block
ExceptionAPI.store(e, new String[][]{{"flag1", "Should be part of flag1"}, {"flag2", "this should be flag 2 contence"}});
}
}
public static void test() throws IOException{
throw new IOException();
}
我不明白为什么这不起作用。我认为这与第二种方法有关,特别是
if(flag != ""){
writer.write("Flags by Developer: ");
writer.write(flag);
}
如果有人能帮我的话,谢谢。
卷曲
如果您只想将字符串数组转换为单个字符串,请尝试以下操作:
String[] stringTwoD = //... I think this is a 1D array, and Sting[][] is a 2D, anyway
String stringOneD = "";
for (String s : stringTwoD)
stringOneD += s;//add the string with the format you want
顺便说一句,你的循环条件似乎是错误的,所以你可以把它改为:
for(int i = 0; i < flags.length; i++){
flag += flags[i][0] + ": " + flags[i][1] + "n";
}