文件读出 文件实例

package test.demo001;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
 * 文件读取写入
 * @author OF
 * 2015年7月7日
 */
public class FileTest {


public static void main(String[] args) {
Buff();
}
/**
* 读取文件<br/>
* 文件UTF-8编码<br/>
* 对文件每一行进行读取
* line 读文件总行数
*/
public static void Buff(){
File f = new File("d:"+File.separatorChar+"of.txt");
InputStream input  = null;
BufferedReader buf= null;
try {
input = new FileInputStream(f);
buf = new BufferedReader(new InputStreamReader(input,"utf-8"));
StringBuffer sb = new StringBuffer();
String text;
String[] tmp = null ;
int line = 0;
String work = "";
while ((text =buf.readLine()) != null){
line ++;
tmp = text.split(",", 10);
work = tmp[1];
String password = MD5.MD5Encode(work+"123456");
System.out.println(password);
}
System.out.println(line);
} catch (Exception e) {
}finally{
try {
buf.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 文件写入<br/>

*/
public static void file(){
File f = new File("d:"+File.separatorChar+"of.txt");
FileOutputStream out = null;
InputStream input  = null;
try {
f.createNewFile();
out = new FileOutputStream(f,true);
String s ="helloword!";
out.write(s.getBytes());
input = new FileInputStream (f);
byte[] temp = new byte[1024];
StringBuffer ss = new StringBuffer();
int len;
while(( len= input.read(temp)) != -1){
ss.append(new String(temp,0,len));

}
System.out.println(ss.toString());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值