• 周二. 5月 21st, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java: file write read operation and tool class

[db:作者]

1月 3, 2022

Example of file reading and writing

package com.demo;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileUtil {

public static void main(String[] args) throws IOException {

// Overwrite write 
PrintWriter writer = new PrintWriter(new FileWriter("data.txt"));
writer.println("hello");
writer.println("world");
writer.close();
// Append write 
PrintWriter appendWriter = new PrintWriter(new FileWriter("data.txt", true));
appendWriter.println("hello");
appendWriter.println("world");
appendWriter.close();
// According to the line read 
List<String> list = new ArrayList<>();
Scanner scanner = new Scanner(new FileReader("data.txt"));
while (scanner.hasNextLine()) {

String line = scanner.nextLine();
list.add(line);
}
scanner.close();
System.out.println(list);
// [hello, world, hello, world]
}
}

Tool class encapsulation

write in

package com.demo.fileutil;
import java.io.IOException;
import java.io.PrintWriter;
public class FileWriter {

private PrintWriter writer;
public FileWriter(String fileName) throws IOException {

this(fileName, false);
}
public FileWriter(String fileName, boolean append) throws IOException {

this.writer = new PrintWriter(new java.io.FileWriter(fileName, append));
}
public void println(String content){

this.writer.println(content);
}
public void print(String content){

this.writer.print(content);
}
public void close(){

this.writer.close();
}
}

Read

package com.demo.fileutil;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileReader {

private String fileName;
public FileReader(String fileName) {

this.fileName = fileName;
}
public List<String> readLines() throws FileNotFoundException {

List<String> list = new ArrayList<>();
Scanner scanner = new Scanner(new java.io.FileReader(this.fileName));
while (scanner.hasNextLine()) {

String line = scanner.nextLine();
list.add(line);
}
scanner.close();
return list;
}
public String readAll() throws IOException {

StringBuilder sb = new StringBuilder();
java.io.FileReader reader = new java.io.FileReader(this.fileName);
int len = 0;
char[] data = new char[1024];
while ((len = reader.read(data)) != -1) {

sb.append(data, 0, len);
}
reader.close();
return sb.toString();
}
}

test

package com.demo;
import com.demo.fileutil.FileReader;
import com.demo.fileutil.FileWriter;
import java.io.IOException;
import java.util.List;
public class TestFileUtil {

public static void main(String[] args) throws IOException {

FileWriter writer = new FileWriter("data.txt", true);
writer.println("hello");
writer.println("world");
writer.close();
FileReader reader = new FileReader("data.txt");
String content = reader.readAll();
System.out.print(content);
// hello
// world
List<String> list = reader.readLines();
System.out.println(list);
// [hello, world]
}
}
《Java: file write read operation and tool class》有125个想法

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注