java在一个文件中进行修改_Java程序从文本文件中读取输入并相应地进行修改

我正在编写一个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());

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值