对于文件的知识了解的很少,工作中对于这部分用的还是比较多的,而且用的还是比较深。自己应该好好学习学习这方面的知识。
/**
* 读取数据
*/
public static String ReadDate() {
String url = "e:/2.txt";
String file=null;
try {
FileReader read = new FileReader(new File(url));
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while(d!=-1){
String str = new String(ch,0,d);
sb.append(str);
d = read.read(ch);
}
System.out.print(sb.toString());
file=sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 写入数据
*/
public static void WriteDate() {
try{
File file = new File("D:/abc.txt");
if (file.exists()) {
file.delete();
}
file.createNewFile();
BufferedWriter output = new BufferedWriter(new FileWriter(file));
ArrayList ResolveList = new ArrayList();
String name=Test.ReadDate();
ResolveList.add(name);
for (int i=0 ;i<ResolveList.size(); i++) {
output.write(String.valueOf(ResolveList.get(i)) + "\n");
}
output.close();
} catch (Exception ex) {
System.out.println(ex);
}
}
/**
* 读取数据
*/
public static String ReadDate() {
String url = "e:/2.txt";
String file=null;
try {
FileReader read = new FileReader(new File(url));
StringBuffer sb = new StringBuffer();
char ch[] = new char[1024];
int d = read.read(ch);
while(d!=-1){
String str = new String(ch,0,d);
sb.append(str);
d = read.read(ch);
}
System.out.print(sb.toString());
file=sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 写入数据
*/
public static void WriteDate() {
try{
File file = new File("D:/abc.txt");
if (file.exists()) {
file.delete();
}
file.createNewFile();
BufferedWriter output = new BufferedWriter(new FileWriter(file));
ArrayList ResolveList = new ArrayList();
String name=Test.ReadDate();
ResolveList.add(name);
for (int i=0 ;i<ResolveList.size(); i++) {
output.write(String.valueOf(ResolveList.get(i)) + "\n");
}
output.close();
} catch (Exception ex) {
System.out.println(ex);
}
}