[英]I need help in java
所以在我正在处理的代码中,它从输入文件中读取并将该信息写入输出文件。 在输入文件中我有这个信息 1245: my address: miami, fl
然后当在输出中写下信息时,我需要它更改 : for |。 谁能解释一下我该怎么做。
import java.io.*;
public class fileConverted {
public static void main(String args[]) {
try {
File input = new File("input");
File output = new File("output");
Scanner sc = new Scanner(input);
PrintWriter printer = new PrintWriter(output);
while (sc.hasNextLine()) {
String s = sc.nextLine();
printer.write(s);
}
printer.flush();
}
catch (FileNotFoundException e) {
System.err.println("File not found. Please scan in new file.");
}
}
}
在写入输出文件之前调用 String.replaceAll() 方法。
char x=f.read();
if(x==':')
x='|';
添加以下行:
s = s.replace(":", "|");
后
String s = sc.nextLine();
请试试这个,它工作得很好
import java.io.*;
import java.util.*;
class fileConverted
{
public static void main(String args[])
{
try
{
File input = new File("input.txt");
File output = new File("output.txt");
Scanner sc = new Scanner(input);
PrintWriter printer = new PrintWriter(output);
while (sc.hasNextLine())
{
String s = sc.nextLine();
printer.write(s.replace(':', '|'));
}
printer.flush();
}
catch (FileNotFoundException e)
{
System.err.println("File not found. Please scan in new file.");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.