Java读写txt文件
2009-11-16 12:41
531 查看
笔试的时候想不起来怎么写了。留个代码作纪念
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); } } }
相关文章推荐
- java读写txt文件
- Java 读写txt文件
- Java读写txt或doc文件
- java读写txt文件中文乱码问题
- Java 文件读写(txt类型读写并追加内容)
- Java读写txt文件中文乱码问题
- paip.文件读写api php java python总结.txt
- java读写txt文件
- java读写TXT文件的方法
- Java读写txt文件中文乱码问题
- Java读写txt文件中文乱码问题
- Java 读写TXT文件
- Java读写txt文件
- Java Android 创建读写TXT文件
- java读写txt文件
- Java读写txt文件
- paip.文件读写api php java python总结.txt
- Java读写txt文件-Java读写文件(txt)
- Java读写txt文件时防止中文乱码问题出现的方法介绍
- java对txt文件和word文档的读写