0

I am writing a Java program that inputs a test file, performs some modifications to the data, then writes it to a new file output.

The input text file looks like this...

url = http://184.154.145.114:8013/wlraac name = wlr samplerate = 44100 channels =2 format = S16le~
url = http://newstalk.fmstreams.com:8080 name = newstalk samplerate = 22050 channels = 1 format = S16le

The program needs to be able to change the samplerate to 44100, and the channels to 1, if they don't already have these values. I would also remove the url and name pieces completely. After these changes, the new line needs to be written out to a different output text file.

So far, all my program can do is select a file and display the contents of the file to the user. Could someone please point me in the right direction for how my program should work to achieve my required outcome.

As somebody asked here is what I have so far

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

public class reader2 {
public reader2() {
}
public static void main(String[] args) {
    reader(args);

}
public static void reader(String[] args) {

    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".txt")
            || f.isDirectory();
        }

        public String getDescription() {
            return "Text Documents (.txt)";
        }
    });

    int r = chooser.showOpenDialog(new JFrame());
    if (r == JFileChooser.APPROVE_OPTION) {
        String name = chooser.getSelectedFile().getName();
        String pathToFIle = chooser.getSelectedFile().getPath();
        System.out.println(name);
        try{
            BufferedReader reader = new BufferedReader( new FileReader( pathToFIle ) ); //Setup the reader

            while (reader.ready()) { //While there are content left to read
                String line = reader.readLine(); //Read the next line from the file
                String[] tokens = line.split( "url = " ); //Split the string at every @ character. Place the results in an array.

                for (String token : tokens){ //Iterate through all of the found results

                        //System.out.println(token);
                        System.out.println(token);
                    }

            }

            reader.close(); //Stop using the resource
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}

}
AlanF
  • 1,591
  • 3
  • 14
  • 20
  • What have you tried? If you can already read the file, then you should be able to read the parts you want and write those to the output file. – jzworkman Apr 12 '12 at 13:37
  • 1
    Show us your code which opens the file and reads its contents. – Marko Topolnik Apr 12 '12 at 13:42
  • Why don't you simply replace all 'samplerate' with 44100 using a regex, regardless of its value. You can do this as you read the file... and then simply write the output. – radimpe Apr 12 '12 at 13:43
  • i have put the code I am using into the question now – AlanF Apr 12 '12 at 13:54

2 Answers2

3

You will need to do something like this ...

  1. Read the contents of the file, one line at a time
  2. Split the line up into the individual components, such as splitting it on the 'space' character
  3. Change the sample rate and channel values according to your question
  4. Write the line out to a file, and start again from step 1.

If you give this a try, post some code on StackExchange with any problems and we'll try to assist.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
2

can you try

        File file = new File( fileName );
        File tempFile = File.createTempFile("buffer", ".tmp");
        FileWriter fw = new FileWriter(tempFile);

        Reader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);

        while(br.ready()) {
            String line = br.readLine();
            String newLine = line.replaceAll( "samplerate =\\s*\\d+", "samplerate = 44100");
            newLine = newLine.replaceAll( "channels =\\s*\\d+", "channels = 1");

            fw.write(newLine + "\n");
        }

        fw.close();
        br.close();
        fr.close();

        // Finally replace the original file.
        tempFile.renameTo(file);

Ref: Files java replacing characters

Community
  • 1
  • 1
Oscar Castiblanco
  • 1,626
  • 14
  • 31