文件输入与复制(判断文件和目录是否存在)、读取网页内容

一、文件的输入和复制:

(判断文件目录/文件是否存在,不存在就创建)

package cn.tset.day04;

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.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class FileDemo {
	static File f = new File("e://javaio.txt");

	public static void main(String[] args) {
		writeFile();
		copeFile();
	}

	/**
	 * 写到文件中
	 */
	public static void writeFile() {

		String s = "1.存储在某种媒体上可加以鉴别的符号资料。即对现实世界中客观事物的符号表示(描述事物的符号记录)." + "2. 对数据的收集、整理、组织、存储、维护、检索、传送等操作,基本目的就是从大量的,"
				+ "杂乱无章的,难以理解的数据中筛选出有意义的数据。" + "3.长期储存在计算机内的有组织的、可共享的相关数据的集合。";
		FileOutputStream fos = null;
		BufferedWriter bw = null;
		PrintWriter pw = null;
		try {
			fos = new FileOutputStream(f);
			bw = new BufferedWriter(new OutputStreamWriter(fos));
			bw.write(s);
//			pw = new PrintWriter(fos);
//			pw.println(s);
//			pw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(bw != null)
					bw.close();
				if (pw != null)
					pw.close();
				if (fos != null)
					fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 复制
	 */
	public static void copeFile() {
		FileInputStream fis = null;
		BufferedReader br = null;
		FileOutputStream fos = null;
		PrintWriter pw = null;
		try {
			fis = new FileInputStream(f);
			br = new BufferedReader(new InputStreamReader(fis));
			String line = br.readLine();

			File f = new File("f://123.txt");
			fos = new FileOutputStream(f);
			pw = new PrintWriter(fos);
			pw.println(line);
			pw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(pw != null)
					pw.close();
				if(fos != null)
					fos.close();
				if(br != null)
					br.close();
				if(fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

对上述代码的封装如下:

package cn.java1606.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class FileDemo {

	public static void main(String[] args) {
		FileDemo demo = new FileDemo();
		String data = "1.存储在某种媒体上可加以鉴别的符号资料。即对现实世界中客观事物的符号表示(描述事物的符号记录)."
				+ "对数据的收集、整理、组织、存储、维护、检索、传送等操作,基本目的就是从大量的,杂乱无章的,"
				+ "难以理解的数据中筛选出有意义的数据。"
				+ "3.长期储存在计算机内的有组织的、可共享的相关数据的集合。复制此文件到电脑F盘。用程序实现上述操作.";
		
		File oldFile = demo.createFile("E:\\java1\\test\\javaio.txt");
		File newFile = demo.createFile("F:\\java1\\test\\javaio.txt");
		
		demo.writeToFile(oldFile, data);
		
		demo.copeFile(oldFile, newFile);
	}

	/**
	 * 根据文件地址创建文件
	 * @param file
	 * @return
	 */
	public File createFile(String file) {
		int index = file.lastIndexOf(File.separator);//字符串的切割(以最后一个\分割)
		String path = file.substring(0, index);
		String name = file.substring(index + 1);

		return createFile(path, name);
	}

	/**
	 * 根据路径和文件名创建文件(如果不用上面createFile(String file)重载方法)
	 * File oldFile = demo.createFile("E:\\java1\\test\\javaio.txt");应改为:
	 * File oldFile = demo.createFile("E:\\java1\\test","javaio.txt")
	 * @return
	 */
	public File createFile(String path, String name) {
		File dir = new File(path);
		if (!dir.exists()) {//判断目录是否存在
			dir.mkdirs();//创建目录
		}

		File file = new File(dir, name);
		if (!file.exists()) {//判断文件是否存在
			try {
				file.createNewFile();//创建文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * 写字符串数据到指定文件
	 * @param file
	 * @param data
	 */
	public void writeToFile(File file, String data) {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		BufferedWriter bw = null;
		try {
			fos = new FileOutputStream(file);
			osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			bw.write(data);

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
				if (bw != null) {
					osw.close();
				}
				if (bw != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

	}
	
	/**
	 * 将文件oldFile复制到newFile
	 * @param oldFile
	 * @param newFile
	 */
	public void copeFile(File oldFile,File newFile){
		try {
			FileInputStream fis = new FileInputStream(oldFile);
			FileOutputStream fos = new FileOutputStream(newFile);
			
			byte[] bytes = new byte[128];
			int len;
			while((len = fis.read(bytes)) != -1){
				fos.write(bytes,0,len);
			}
			
			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}


读取百度网页内容显示到控件台界面。

package cn.tset.day04;

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

public class UrlDemo {

	public static void main(String[] args) {
		InputStreamReader isr = null;
		try {
			URL url = new URL("https://www.baidu.com");
			URLConnection uc = url.openConnection();
			
			InputStream is = uc.getInputStream();
			isr = new InputStreamReader(is);
			
			int c ;
			while((c=isr.read())!=-1){
				System.out.print((char)c);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(isr != null)
					isr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值