文件的拷贝方法

爬虫demo:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class Spider {

	public static void main(String[] args) throws IOException {
		
		spider("433009");
		
	}

	private static void spider(String postcode) throws IOException {
		URL url = new URL("http://tool.cncn.com/youbian/" + postcode);
		URLConnection urlConnection = url.openConnection();
		urlConnection.setRequestProperty(
				"User-Agent",
				"Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
		InputStream inputStream = urlConnection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "gb2312"));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		while ((line = reader.readLine()) != null) {
			buffer.append(line);
		}
		System.out.println(buffer);
	}
	
}



使用BufferReader拷贝文件的方法

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.jerehsoft.ch05.copy.CoptFileUtil;

public class BufferedCopy {

	private static FileWriter fw;
	private static FileReader fr;
	private static BufferedWriter bw;
	private static BufferedReader br;

	public static void copyFile(String source, String target) {

		try {
			fw = new FileWriter(source);
			fr = new FileReader(target);
			bw = new BufferedWriter(fw);
			br = new BufferedReader(fr);
			int c = br.read();
			char[] info = new char[1024];
			while ((c = br.read(info)) != -1) {
				bw.write(new String(info, 0, c));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		CoptFileUtil.copyFile("E:\\考勤表.xls", "D:\\考勤表.xls");
	}

}

字符流的写入和读取例子,不会出现乱码

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class BufferedReaderDemo {

	public static void main(String[] args) {

		// 字符流的写入,字节流写汉字可能出错
		try {
			String str = "andau,23,hello1,啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 ";
//			String filePath = BufferedReaderDemo.class.getResource("/").getPath();
//			filePath += "readme2.txt";
			FileWriter fw = new FileWriter("E:\\写入内容.txt");
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(str);
			// 不关闭则不会写入
			bw.close();fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		//字符流的读取
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\读取内容.txt"), "GBK"));
			StringBuffer sb = new StringBuffer();
			int len=0;
			char[] info = new char[1024];
			while ((len = br.read(info)) != -1) {
				sb.append(new String(info, 0, len));
			}
			System.out.println(sb);
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}



字节流的写入和读取例子,汉子会出现乱码

import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * 文件流的读取,此程序正确
 * 
 */
public class FileInputStream2 {

	public static void main(String[] args) {
		
		try {
			String str = "nidayu,23,啊啊啊啊啊啊啊啊啊啊";
//			String filePath = FileOutputStreamDemo.class.getResource("/").getPath();
			FileOutputStream fos = new FileOutputStream("E:\\写入内容.txt");
			fos.write(str.getBytes());
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			// 获取项目所在路径
			FileInputStream fis = new FileInputStream("E:\\读取内容.txt");
			int c = 0;
			StringBuffer sb = new StringBuffer();
			// 适合信息量比较大的文件
			byte[] b = new byte[1024];
			while ((c = fis.read(b)) != -1) {
				sb.append(new String(b,0,c));
			}
			System.out.println(sb);
			fis.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


文件使用的工具类

package com.jerehsoft.homework.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileIOUtil {

	private static FileInputStream fis;
	private static FileOutputStream fos;
	private static long DRISIZE = 0;

	// 实现文件的拷贝
	public static void copy(File f1, String target) {
		try {
			fis = new FileInputStream(f1);
			fos = new FileOutputStream(target);
			int c = fis.read();
			while (c != -1) {
				fos.write((char) c);
				c = fis.read();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 实现文件的移动
	public static void move(File f1, String target) {
		copy(f1, target);
		del(f1);
	}

	// 文件的删除
	public static void del(File f) {
		if (f.exists()) {
			f.delete();
		}
	}

	// 文件的创建
	public static void createFile(File f) {
		if (!f.exists() && !f.isDirectory()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 显示文件的大小信息
	public static void fileInfo(File f) {
		if (f.exists()) {
			if (f.length() >= 1024 * 1024) {
				System.out.println("文件大小为:" + f.length() / 1024 / 1024 + "MB");
			} else if (f.length() >= 1024 && f.length() < 1024 * 1024) {
				System.out.println("文件大小为:" + f.length() / 1024 + "KB");
			} else {
				System.out.println("文件大小为:" + f.length() + "B");
			}
		}
	}

	// 显示文件的所有信息
	public static void printFileInfo(File f) {
		System.out.println("文件名:" + f.getName());
		System.out.println("路径:" + f.getPath());
		System.out.println("绝对路径:" + f.getAbsolutePath());
		System.out.println("是否可读:" + f.canRead());
		System.out.println("是否可写:" + f.canWrite());
		fileInfo(f);
	}

	// 文件目录信息显示
	public static void fileDirInfo(File f) {
		if (f.exists() && f.isDirectory()) {
			File[] ff = f.listFiles();
			for (int i = 0; i < ff.length; i++) {
				long time = ff[i].lastModified();
				Date d = new Date();
				d.setTime(time);
				SimpleDateFormat sdf = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:SS");
				if (ff[i].isDirectory()) {
					System.out.println(ff[i].getName() + "\t目录\t"
							+ sdf.format(d));
				} else {
					System.out.println(ff[i].getName() + "\t文件\t"
							+ sdf.format(d));
				}
			}
		}
	}

	// 显示文件夹的大小
	public static long dirSizeInfo(File f) {
		if (!f.isDirectory()) {
			DRISIZE += f.length();
		} else {
			File[] ff = f.listFiles();
			for (int i = 0; i < ff.length; i++) {
				//不能写return
				dirSizeInfo(ff[i]);
			}
		}
		return DRISIZE;
	}

	public static long driSize(File file) {
		if (file.exists()) {
			if (file.isDirectory()) {
				File[] files = file.listFiles();
				for (int i = 0; i < files.length; i++) {
					driSize(files[i]);
				}
			} else {
				DRISIZE += file.length();
			}
		}
		return DRISIZE;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值