Java——第六章(异常处理和文件IO流操作)—项目案例

时间:2022-03-22 21:36:27


1.. x:/a/b/c  创建一个文件 1.doc, 键盘输入两个字符串, 写入1.doc中, 两字符串在文件中是两行,    


你从1.doc中全部读出来,把每一个字符串都颠倒  写入2.doc中, 从2.doc  在读出来打印。


代码:



import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Demo05 {
	public static final String path = "G:/a/b/c";
	public static final String path1 = "G:/a/b/c/1.doc";
	public static final String path2 = "G:/a/b/c/2.doc";

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		System.out.println("输入两个字符串");
		String str = sc.next();
		String str1 = sc.next();
		CreatDir(path);
		String contant = str + "\r\n" + str1;
		FileOutputStream fos = new FileOutputStream(path1);
		Write(fos, contant);

		FileInputStream fis = new FileInputStream(path1);
		String contant1 = Read(fis);

		String[] ss = contant1.split("\n");
		String line1 = ss[0];
		String line2 = ss[1];
		System.out.println("第一个字符串: " + line1);
		System.out.println("第二个字符串: " + line2);
		System.out.println("第一个字符串颠倒后: " + DianDao(line1));
		System.out.println("第二个字符串颠倒后: " + DianDao(line2));

		FileOutputStream fos1 = new FileOutputStream(path2,true);
		String ss1 = line1+"\r\n"+line2;
		Write(fos1, ss1);
		

	}

	public static String Read(FileInputStream fis) throws IOException {
		byte[] b = new byte[1024];
		int len;
		String contant1 = "";
		while ((len = fis.read(b)) != -1) {
			contant1 += new String(b, 0, len);
		}
		System.out.println(contant1);
		return contant1;
	}

	public static void CreatDir(String path) {
		new File(path).mkdirs();
	}

	public static void CreatFile(String path) {
		try {
			new File(path).createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void Write(FileOutputStream fos, String contant) {

		try {
			fos.write(contant.getBytes());
			fos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public static String DianDao(String str) {
		char[] c = str.toCharArray();
		for (int i = 0; i < c.length / 2; i++) {
			char temp = c[i];
			c[i] = c[c.length - 1 - i];
			c[c.length - 1 - i] = temp;
		}
		return new String(c);

	}

}

同样可以用字符流做。


代码:



import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Demo09 {
	public static final String path="G:/a/b/c";
	public static final String path1="G:/a/b/c/1.doc";
	public static final String path2="G:/a/b/c/2.doc";
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		CreatDir(path);
		CreatFile(path1);
		
		Scanner sc = new Scanner(System.in);
		String str = sc.next();
		String str1=sc.next();
		String contant = str+"\r\n"+str1+"\r\n";
		FileWriter fw = new FileWriter(path1,true);
		Write(fw, contant);
		fw.close();
		
		FileReader fr = new FileReader(path1);
		String s =Read(fr);
		
		String []ss =s.split("\n");
		String line1 = ss[0];
		String line2 = ss[1];
		
		FileWriter fw1 = new FileWriter(path2,true);
		
		
		Write(fw1, DiaoDao(line1));
		Write(fw1, DiaoDao(line2));
		
		fw1.close();
		
		FileReader fr1 = new FileReader(path2);
		Read(fr1);
		
		
		
		
		
		
		
		
		
		
	}
	/*
	 * 创建目录
	 */
	public static void CreatDir(String path)
	{
		new File(path).mkdirs();
	}
	//创建文件
	public static void CreatFile(String path)
	{
		try {
			new File(path).createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//写方法
	public static void Write(FileWriter fw,String contant)
	{
		try {
			fw.write(contant);
			fw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	//读方法
	public static String Read(FileReader fr) throws Exception
	{
		char c[]= new char[1024];
		int len;
		String ss="";
		while((len=fr.read(c))!=-1)
		{
			ss+=new String(c,0,len);
		}
		System.out.println(ss);
		return ss;
	}
	//字符串颠倒
	public static String DiaoDao(String str){
		char[] c=str.toCharArray();
		for (int i = 0; i < c.length/2; i++) {
			char temp = c[i];
			c[i]=c[c.length-1-i];
			c[c.length-1-i]=temp;
		}
		return new String(c);
	}
	

}


2..编写一个程序,其功能是将两个文件的内容合并到一个文件中。


代码:



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;

public class Demo16 {

	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		
		/*
		 * 编写一个程序,其功能是将两个文件的内容合并到一个文件中。
		 */
		File fromfile = new File("G:/a/b/c/1.doc");
		File tofile = new File("G:/a/b/c/2.doc");
		FileReader fr = new FileReader(fromfile);
		FileWriter fw = new FileWriter(tofile,true);
		BufferedReader br = new BufferedReader(fr);
		BufferedWriter bw = new BufferedWriter(fw);
		
		String s ="";
		while((s=br.readLine())!=null)
		{
		  bw.write(s);
		  bw.flush();
		  fw.flush();
		  
		}
		br.close();
		fr.close();
		bw.close();
		fw.close();
}
}


3..编写一个程序实现以下功能:

①产生5000个1~9999之间的随机整数,将其存入文本文件a.txt中。

②从文件中读取这5000个整数,并计算其最大值、最小值和平均值并输出结果。


代码:



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Random;

public class Demo17 {

	public static void main(String[] args) throws Exception {

		/*
		 * 编写一个程序实现以下功能:()考虑内存优化
	①产生5000个1~9999之间的随机整数,将其存入文本文件a.txt中。
	②从文件中读取这5000个整数,并计算其最大值、最小值和平均值并输出结果。
		 */
		
		Random r = new Random();
		File file = new File("G:/a/b/c/a.txt");
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		
		for (int i = 0; i < 5000; i++) {
			bw.write((r.nextInt(9999)+1)+"");
			bw.newLine();
			bw.flush();
			fw.flush();
		
		}
		bw.close();
		fw.close();
		
		FileReader fr = new FileReader(file);
		BufferedReader br = new BufferedReader(fr);
		String s="";
		int index=0;
		int a[] = new int [5000];
		while((s=br.readLine())!=null)
		{
			System.out.println(s);
			a[index] = Integer.valueOf(s);
			index++;
		}
		
		System.out.println("最大值:"+GetMax(a));
		br.close();
		fr.close();
		
		
		
		
		
		
	}
	public static int GetMax(int [] a)
	{
		int max = a[0];
		for (int i = 1; i < a.length; i++) {
			if(a[i]>max)
			{
				max =a[i];
			}
		}
		return max;
	}

}


4..从网上下载一个软件,例如QQ:(属于了解)


代码:



import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class DownLoad {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		
		long time = System.currentTimeMillis();
		String add = "http://sw.bos.baidu.com/sw-search-sp/software/e6207d37846ba/QQ_8.7.19113.0_setup.exe";
		URL url = new URL(add);
		HttpURLConnection huc =  (HttpURLConnection) url.openConnection();
		InputStream is = huc.getInputStream();
		File file = new File("g:/yunke/QQ.exe");
		FileOutputStream fos = new FileOutputStream(file,true);
		
		byte []	b = new byte[1024*1024*500];
		int len;
		double leijia = 0.0;
		long total= huc.getContentLengthLong();
		while((len = is.read(b) )!=-1)
		{
			fos.write(b, 0, len);
			fos.flush();
			leijia+=len;
			System.out.println("已下载"+(leijia/total)*100+"%");
		}
		long time1 = System.currentTimeMillis();
		fos.close();
		
		System.out.println("耗时:"+(time1-time)+"ms");
	}

}


运行结果:


Java——第六章(异常处理和文件IO流操作)—项目案例



5..从网上下载小说,古诗,然后将内容即中文内容选出,打印出来。(一首古诗)


代码:



import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Download1 {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		URL url = new URL("http://www.diyifanwen.com/sicijianshang/tangshisanbaishou/044407104270444076210955.html");
		HttpURLConnection hur = (HttpURLConnection) url.openConnection();
		InputStream is = hur.getInputStream();
		
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String s = "";
		
		File file = new File("g:/a/b/c/3.doc");
		FileWriter fw = new FileWriter(file);
		BufferedWriter bw = new BufferedWriter(fw);
		
		while((s=br.readLine())!=null)
		{
			char c[] = s.toCharArray();
			String ss= "";
			for (int i = 0; i < c.length; i++) {
				if(c[i]>255||c[i]<0)
				{
					ss+=c[i];
				}
			}
			if (!ss.equals("")) {
				System.out.println(ss);
				bw.write(ss);
				bw.flush();
				fw.flush();
			}
			
		}
		br.close();
		isr.close();
		bw.close();
		fw.close();
	}
	
	
	public static String getZhongWen(String temp) {
		char[] c = temp.toCharArray();
		String str = "";
		for (int i = 0; i < c.length; i++) {
			if (!(c[i] >= 0 && c[i] <= 255)) {
				str += c[i];
			}
		}
		return str;
	}

}


运行结果:


Java——第六章(异常处理和文件IO流操作)—项目案例