public String getText()
{
ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-server.xml"});
Resource resource = appContext.getResource("file:D:\\text\\test.txt");
StringBuilder builder = new StringBuilder();
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
PrintWriter out=null;
while ((line = br.readLine()) != null) {
//System.out.println(line);
out = new PrintWriter(new FileWriter("D:\\outputfile.txt"));
out.println(line);
//br.close();
}
out.close();
br.close();
}catch(IOException e){
e.printStackTrace();
}
return builder.toString();
}
答案 0 :(得分:1)
在while循环中创建PrintWriter实例。
答案 1 :(得分:1)
您正在循环中创建新的PrintWriters。把它放在外面。
out = new PrintWriter(new FileWriter("D:\\outputfile.txt"));
while ((line = br.readLine()) != null) {
//System.out.println(line);
out.println(line);
//br.close();
}
答案 2 :(得分:1)
将它移到循环之外。每行创建new
个实例
out = new PrintWriter(new FileWriter("D:\\outputfile.txt"));
答案 3 :(得分:0)
在每次循环迭代期间,PrintWriter
被分配给新实例。在循环之外声明PrintWriter
。
String line;
PrintWriter out= new PrintWriter(new FileWriter("D:\\outputfile.txt"));
while ((line = br.readLine()) != null) {
out.println(line);
}