Java程序从文本文件中读取输入并相应地进行修改

时间:2012-04-12 13:33:31

标签: java string file-io

我正在编写一个Java程序,它输入一个测试文件,对数据执行一些修改,然后将其写入新的文件输出。

输入文本文件如下所示......

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

如果没有这些值,程序需要能够将采样率更改为44100,将通道更改为1。我也会删除网址并完全命名。完成这些更改后,需要将新行写出到不同的输出文本文件中。

到目前为止,我的所有程序都可以选择一个文件并将该文件的内容显示给用户。有人可以指出我正确的方向,我的计划应该如何运作 达到我要求的结果。

正如有人问我到目前为止我所拥有的

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

}

2 个答案:

答案 0 :(得分:3)

你需要做这样的事情......

  1. 一次读取一行文件的内容
  2. 将该行拆分为各个组件,例如将其拆分为“空格”字符
  3. 根据您的问题更改采样率和通道值
  4. 将换行写入文件,然后从步骤1重新开始。
  5. 如果您尝试一下,请在StackExchange上发布一些代码并解决任何问题,我们会尝试提供帮助。

答案 1 :(得分:2)

你可以尝试

吗?
        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);

参考:Files java replacing characters

相关问题