[英]Java Program read input from a text file and modify it accordingly
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. 我正在编写一个Java程序,该程序输入一个测试文件,对数据进行一些修改,然后将其写入新的文件输出中。
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. 该程序需要能够将采样率更改为44100,将通道更改为1(如果它们尚未具有这些值)。 I would also remove the url and name pieces completely. 我还将完全删除url和名称块。 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());
}
}
}
}
You will need to do something like this ... 您将需要执行以下操作...
If you give this a try, post some code on StackExchange with any problems and we'll try to assist. 如果尝试一下,请将任何有问题的代码发布到StackExchange上,我们将尽力提供帮助。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.