在web项目中,如何用java代码实现文件、数据,等等的导入、导出?求助,菜鸟级。。。 万分感谢!
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;
}
}
}
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;
}
}
}
更多追问追答
追问
我运行了,来试了一下, 没反应啊、 怎么回事?》 谢谢!
追答
看看d:/java.txt
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |