我发现在Java中执行以下操作有些困难:
我必须获取xml文件的内容并将其打印
我这样做:
System.out.println("settings.xml: " + ClassLoader.getSystemResourceAsStream("/home/andrea/Documenti/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/src/settings.xml"));
问题是这个陈述的结果是:
settings.xml: null
为什么呢?我能做些什么呢?
TNX
安德烈
答案 0 :(得分:2)
您可以使用此功能:
private String getStringFromFile(File file)
{
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try
{
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
while ((line = br.readLine()) != null)
{
sb.append(line);
}
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return sb.toString();
}
答案 1 :(得分:0)
例如:
System.out.println("settings.xml: " + ClassLoader.getSystemResourceAsStream("home/andrea/Documenti/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/src/settings.xml"));