0

我正在尝试读取一个文件并写入另一个不起作用的文件我从主调用该方法

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;
}

}

4

3 回答 3

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();
于 2013-10-25T20:13:29.680 回答
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;
    }
}
于 2013-10-25T20:18:06.293 回答
0

试试Apache Commons IO Utils

FileUtils.copyFile(new File(inputPlayList),new File(outputPlayList));
于 2013-10-25T20:05:32.687 回答