0

I am trying to read a file and write to another file it is not working I invoke the method from the main

public boolean copy(String inputPlayList, String outputPlayList, int numberOfMunites) 
{
     String start1 = "#EXTINF:";
     String afterNum = ";";

    try
    {

declaring those variable that I would use to pass the method

        File fInput, fOutput; 
        String s;    
        String a;

assigning those variable to the method

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

}

kosa
  • 65,990
  • 13
  • 130
  • 167

3 Answers3

1

Simplest way in 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();
FDIM
  • 1,981
  • 19
  • 21
  • Yep. Only thing I would change would be to use buffered streams and declare the `byte[]` to be at least 512K instead of just 4K. – MadConan Oct 25 '13 at 20:19
  • That value depends on the context, for large file download/copying - it should be increased. Not sure if BufferedInputStream would increase performance, I don't think it's necessary if you just have to copy data. – FDIM Oct 25 '13 at 22:11
0

Try Apache Commons IO Utils.

FileUtils.copyFile(new File(inputPlayList),new File(outputPlayList));
0

Here it is, but I don't understand the meaning of the numberOfminutes argument, what is it for? I've changed implementation to return calculated number of minutes from the function.

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;
    }
}
Eddie Jamsession
  • 1,006
  • 6
  • 24
  • Thanks EddieJamession, This is what I am trying to do:This method should create a valid playlist with the outputPlayList filename. It should contain the same tracks as the inputPlayList, in the same order and with the same meta-data, but the total number of minutes should not exceed numberOfMinutes, i.e. the output list should be finished if the inclusion of the next track would exceed this value. Test your method from a main method that loads in playlist. – dan2892309 Oct 29 '13 at 11:56