How to create an file and copy the content from another file into the created file in java?
本问题已经有最佳答案,请猛点这里访问。
我正试图读取一个文件并将其写入另一个文件。它不起作用。我从主文件调用该方法
1 2 3 4 5 6 7 |
声明将用于传递方法的变量
将这些变量赋给方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | fInput = new File(inputPlayList); fOutput = new File(outputPlayList); // Now I am using bufferedRead and BufferedWriter to read and write in a file BufferedReader br = new BufferedReader(new FileReader(new File(inputPlayList))); BufferedWriter out = new BufferedWriter(new BufferedWriter(new FileWriter(outputPlayList))); // creating a while saying while the line is not finish contunue to read while((s = br.readLine())!= null) { if(s.contains(start1)) { String numberInString = s.substring(start1.length(), s.indexOf(afterNum)); numberOfMunites+= Integer.getInteger(numberInString); } // when it is finsh close the file. out.write(s); } out.close(); System.out.println("donne"); }catch ( IOException e) { System.err.println("the is an erro that need to be fixed"+e); } return false; } |
}
Java的单纯方式:
ZZU1
这里是,但我不明白数字论据的含义,它的目的是什么?我改变了执行方式,从功能上计算了几分钟。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.*; public class Main { public static void main(String[] args) { System.out.println(copy("D:\\1.txt","D:\\2.txt", 0)); //returns the calculated number of minutes } public static int copy(String inputPlayList, String outputPlayList, int numberOfMinutes) { String start1 ="#EXTINF:"; String afterNum =";"; try { BufferedReader br = new BufferedReader(new FileReader(new File(inputPlayList))); PrintWriter out = new PrintWriter(new FileWriter(outputPlayList)); String s; while ((s = br.readLine()) != null) { if (s.contains(start1)) { String numberInString = s.substring(start1.length(), s.indexOf(afterNum)); numberOfMinutes += Integer.parseInt(numberInString); } out.println(s); } out.close(); } catch (IOException e) { System.err.println("Exception" + e); } return numberOfMinutes; } } |
尝试用共同的语言。