我正在尝试读取文件并写入另一个文件但它无法正常工作我从主
调用该方法public boolean copy(String inputPlayList, String outputPlayList, int numberOfMunites)
{
String start1 = "#EXTINF:";
String afterNum = ";";
try
{
声明我将用于传递方法的变量
File fInput, fOutput;
String s;
String a;
将这些变量分配给方法
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;
}
}
答案 0 :(得分:1)
java中最简单的方法:
File input = new File("input/file");
File output = new File("output/file");
InputStream is = new FileInputStream(input); // can be any input stream, even url.open()
OutputStream os = new FileOutputStream(output);
byte[] buffer = new byte[4096];//
int read = 0;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
is.close();
os.close();
答案 1 :(得分:0)
FileUtils.copyFile(new File(inputPlayList),new File(outputPlayList));
答案 2 :(得分:0)
在这里,但我不明白numberOfminutes参数的含义,它是什么用的?我已经改变了实现,从函数返回计算的分钟数。
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;
}
}