在web项目中,如何用java代码实现文件、数据,等等的导入、导出?求助,菜鸟级。。。 万分感谢!

2024-12-27 09:58:51
推荐回答(1个)
回答1:

给你个小例子,你参考下

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Admin {

public static void main(String... args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入:");

List result = new ArrayList();
for (int i = 0; i < 3; i++) {
result.add(in.next());
}

String file = "d:/java.txt";
try {
write(file, result);
read(file);
} catch (IOException e) {
e.printStackTrace();
}
}

private static void write(String file, List result) throws IOException {

FileWriter bw = new FileWriter(new File(file));

for (int i = 0; i < result.size(); i++) {
bw.write(String.valueOf(result.get(i)) + "\r\n");
}
bw.close();
}

private static void read(String fileName) {

File file = new File(fileName);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
System.out.println(tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}