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);
}
}
}
相关文档:
Java NIO API详解
在JDK
1.4以前,Java的IO操作集中在java.io这个包中,是基于流的阻塞(blocking)API。对于大多数应用来说,这样的API使用很方
便,然而,一些对性能要求较高的应用,尤其是服务端应用,往往需要一个更为有效的方式来处理IO。从JDK 1.4起,NIO
API作为一个基于缓冲区,并能提供非阻塞(non-blo ......
这里我们再理解了AJAX后,开始来用实例感受AJAX的力量。
今天我最后要实现的效果,当鼠标放到图片上时会根据,会把数据库库里的数据读出,通过显示框显示出来。这个在很多网上商店都有用到这里效果,我们这里用AJAX来实现这个效果。这个实例里结合了MySql、Servlet还有Js,理论性很少,但通过实践来感受理论知识。
......
package chape8;
/**
* 第8章
* 第46條:
* for-each循環優先于for循環
* for(Element e : c)
* {
* dosomthing(e);
* }
* 这个是java1.5以上的版本的for循环的首先考虑的写法
* 以下是java1.5以前的寫法
* for (Iterator it = c.iterator();it.hasNext();)
{ ......
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 ......
import java.util.Arrays;
/**
* 求一个数组的全排列算法
* @author Administrator
*/
public class Pai {
public void pai(char[] array,int start,int end){
System.out.println(" -- 组合 "+start+" 到 "+end+" --");
if(start==end){
  ......