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

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 に答える