java的I/O操作,读取和写入文件

 

package up.down.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.Buffer;
import java.util.Date;

import org.apache.axis.message.InputStreamBody;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.upload.DiskFile;

public class Test {
   public static void main(String[] args) {
   
 File file = new File("./build/期 日 未 収 入 金 内 訳 帳.xml");
 File writefile = new File("./build/writefile.txt");
 String upload="F:/期 日 未 収 入 金 内 訳 帳.xml.bak";
 if(file.exists()){
  System.out.println("--->文件存在");
 try {
  readfile(file);         //读取 
  writfile(writefile,readfile(file)); //创建文件写入
  //uploadfile(upload);  //上传文件
 } catch (Exception e) {
  e.printStackTrace();
 }
 }else{
  System.out.println("-->文件不存在exists");
 }
}
   //读取文件
   public static StringBuffer readfile(File file) throws IOException{
  System.out.println("读取开始时间:"+new Date());
   BufferedReader br = new BufferedReader(new FileReader(file));
  System.out.println("文件大小:"+file.length()/1000+"KB");
   StringBuffer line = new StringBuffer();
    String str = null;
    while((str=br.readLine())!=null){
     line.append(str);
     line.append("\n");
    }
    System.out.println("--->"+line);
    System.out.println("读取结束时间="+new Date());
   br.close();
   return line;
   }
   //写入文件将读取的文件内容保存在制定的工程目录下
   public static void writfile(File file,StringBuffer sb) throws Exception{  
   FileWriter fw = new FileWriter(file);
   fw.getEncoding().getBytes("utf-8");
   fw.write(sb.toString());
   System.out.println("------->写入成功!");
   System.out.println("写入了一个大小为"+file.length()/1000+"KB的文件");
    fw.close();
   }
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大龄牛码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值