Java 读取txt并操作
Java read txt and manipulate
我有一个正在开发的程序,我想对 txt 文件执行以下操作
- 操作下面的字符串abc#。fruit.date
- 将 # 更改为 1 到 9 之间的数字
- 将水果更改为苹果等水果类型
- 将日期更改为 YYYYMMDD 格式的当前日期
- 将字符串值存储到变量中以供程序的其他部分使用
我现在只有它的骨头和肉,因为我正在努力开发算法
代码如下
import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileReader;
import Java.util.Scanner;
public class Text {
public static void main(String [ ] args) {
try (Scanner s = new Scanner(new BufferedReader(new FileReader("Test.txt")))); {
while (s.hasNext()) {
System.out.println(s.next());
}
} catch (FileNotFoundException ex) {
System.out.println("unable to open file");
}
}
}
Text.txt 长得像
abc#.fruit.date
你需要String
class方法replace
和Calendar.getInstace
。然后SimpleDateFormat
这将进行适当的替换并将结果打印到 System.out。文本文件的所有转换行都将在 List<String> output
.
中
public static void main(String args[]) {
String fruits[] = {"Apple", "Orange", "Banana"}; //put whatever fruit values you want in here
List<String> output = new ArrayList<>();
try (Scanner s = new Scanner(new BufferedReader(new FileReader("Test.txt")))) {
while (s.hasNext()) {
String line = s.nextLine();
line = line.replaceAll("#", String.valueOf(randomNumber(1, 9)));
line = line.replaceAll("fruit", fruits[randomNumber(0, fruits.length-1)]);
line = line.replaceAll("date", new SimpleDateFormat("YYYYMMDD").format(new Date()));
output.add(line);
}
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file.");
}
for (String line : output) {
System.out.println(line);
}
}
private static int randomNumber(int min, int max) {
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
输入:
abc#.fruit.date
示例输出:
abc7.Apple.201508223
我有一个正在开发的程序,我想对 txt 文件执行以下操作
- 操作下面的字符串abc#。fruit.date
- 将 # 更改为 1 到 9 之间的数字
- 将水果更改为苹果等水果类型
- 将日期更改为 YYYYMMDD 格式的当前日期
- 将字符串值存储到变量中以供程序的其他部分使用
我现在只有它的骨头和肉,因为我正在努力开发算法 代码如下
import java.io.BufferedReader;
import java.io.FileNotFoundException; import java.io.FileReader;
import Java.util.Scanner;
public class Text {
public static void main(String [ ] args) {
try (Scanner s = new Scanner(new BufferedReader(new FileReader("Test.txt")))); {
while (s.hasNext()) {
System.out.println(s.next());
}
} catch (FileNotFoundException ex) {
System.out.println("unable to open file");
}
}
}
Text.txt 长得像
abc#.fruit.date
你需要String
class方法replace
和Calendar.getInstace
。然后SimpleDateFormat
这将进行适当的替换并将结果打印到 System.out。文本文件的所有转换行都将在 List<String> output
.
public static void main(String args[]) {
String fruits[] = {"Apple", "Orange", "Banana"}; //put whatever fruit values you want in here
List<String> output = new ArrayList<>();
try (Scanner s = new Scanner(new BufferedReader(new FileReader("Test.txt")))) {
while (s.hasNext()) {
String line = s.nextLine();
line = line.replaceAll("#", String.valueOf(randomNumber(1, 9)));
line = line.replaceAll("fruit", fruits[randomNumber(0, fruits.length-1)]);
line = line.replaceAll("date", new SimpleDateFormat("YYYYMMDD").format(new Date()));
output.add(line);
}
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file.");
}
for (String line : output) {
System.out.println(line);
}
}
private static int randomNumber(int min, int max) {
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
输入:
abc#.fruit.date
示例输出:
abc7.Apple.201508223