Java读写txt文件
笔试的时候想不起来怎么写了。留个代码作纪念
package common;
import java.io.*;
import java.util.ArrayList;
public class IOTest {
public static void main (String args[]) {
ReadDate();
WriteDate();
}
/**
* 读取数据
*/
public static void ReadDate() {
String url = "e:/2.txt";
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());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 写入数据
*/
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();
for (int i = 0; i < 10; i++) {
ResolveList.add(Math.random()* 100);
}
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);
}
}
}
相关文档:
今日读到csdn的新闻《欲为Java技术大牛所需的25个学习要点》,了解要做一个Java技术大牛可不是一般一般的,特收录在此:以鞭策自己不断学习:
1. 你需要精通面向对象分析与设计(OOA/OOD)、涉及模式(GOF,J2EEDP)以及综合模式。你应该了解UML,尤其是class、object、interaction以及statediagrams。
2. 你需要学习Java语言 ......
public List createArrayList(String FilePath,String FileName){
List list =new ArrayList();
FileName=FilePath+"\\"+FileName;
File file=new File(FileName);
String encoding="GBK";//防止乱码 这编码需要根据自己的操 ......
Character Classes
[abc] Simple character class
[a-zA-Z]   ......
java exception 解决方案 - 我的异常网|异常|exception 791 - java.lang.NoSuchMethodError 792 - RuntimeException 793 - org.hibernate.exception.SQLGrammarException 794 - Internal Error 795 - 自定义异常 796 - org.dom4j.DocumentException 797 - java.net.SocketException 798 - Exception对象 799 - SQLE ......
在进入我们今天的话题前,先来看看下面这段代码,你知道它的结果吗?
程序代码
public class StringTest1{
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
&nbs ......