Java实验3rd:异常处理与文件操作

目录

一、除法...1

1、源程序...1

2、运行结果...2

二、论坛用户实例化...2

1、J_User.java.2

2、J_ObjectInputStream.java.4

3、J_ObjectOutputStream.java.5

4、运行结果...5

一、除法

1、源程序

import java.io.FileOutputStream;

import java.io.IOException;

public class DivFile {

public static void main(String[] args) {

// 输入两个整数

try {

int a = Integer.parseInt(args[0]);

int b = Integer.parseInt(args[1]);

// 输出结果到文件

String outStr = a + "除以" + b +":商为" + a / b +",余数为" + a % b;

byte[] byteArr = outStr.getBytes();

FileOutputStream f = new FileOutputStream("out.txt");

f.write(byteArr);

f.flush();

f.close();

}

// catch(NumberFormatException e) {

// System.err.println("输入错误!");

// } catch(ArrayIndexOutOfBoundsException e) {

// System.err.println("输入错误!");

// }

catch (ArithmeticException e) {

System.err.println("除数不能为0");

} catch (IOException e) {

System.err.println("文件输出错误!");

} catch (Exception e) {

System.err.println("输入错误!");

}

}

}

2、运行结果

命令行输入:

输出的文件内容:

二、论坛用户实例化

1、J_User.java

import java.io.Serializable;

public class J_User implements Serializable{

privatestatic final long serialVersionUID = 1L;

@Override

/**

* 输出用户名

*/

publicString toString() {

returnthis.username;

}

/**

* 用username和password构造对象。

*

* @param username

* @param password

*/

publicJ_User(String username, String password) {

super();

this.username= username;

this.password= password;

}

/**

* 用所以信息构造对象。

*

* @param username

* @param password

* @param email

* @param realName

* @param website

* @param location

* @param birthday

* @param aboutMe

*/

publicJ_User(String username, String password, String email,

StringrealName, String website, String location, String birthday,

StringaboutMe) {

super();

this.username= username;

this.password= password;

this.email= email;

this.realName= realName;

this.website= website;

this.location= location;

this.birthday= birthday;

this.aboutMe= aboutMe;

}

/**

* 显示个人信息,但不包括密码

*/

publicvoid displayInfo() {

System.out.println("Yourinformation:");

System.out.println("username:" + username);

System.out.println("email:" + email);

System.out.println("realName:" + realName);

System.out.println("website:" + website);

System.out.println("location:" + location);

System.out.println("birthday:" + birthday);

System.out.println("aboutMe:" + aboutMe);

}

Stringusername = ""; // 防止输出null

Stringpassword = "";

Stringemail = "";

StringrealName = "";

Stringwebsite = "";

Stringlocation = "";

Stringbirthday = "";

StringaboutMe = "";

}

2、J_ObjectInputStream.java

import java.io.FileInputStream;

import java.io.ObjectInputStream;

public class J_ObjectInputStream {

public static void main(String[] args) {

try {

ObjectInputStream ff = newObjectInputStream(new FileInputStream(

"user.txt"));

J_User u = (J_User) (ff.readObject());

u.displayInfo();

ff.close();

} catch (Exception s) {

System.out.println("发生异常:" + s);

s.printStackTrace();

}

}

}

3、J_ObjectOutputStream.java

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

public class J_ObjectOutputStream {

public static void main(String[] args) {

try {

ObjectOutputStream f = new ObjectOutputStream(new FileOutputStream(

"user.txt"));

J_User me = new J_User("Justme0","123", "",

"", "blog.csdn.net/justme0", "", "", "Hello!");

f.writeObject(me);

me.displayInfo();

f.close();

} catch (Exception e) {

System.out.println("写入有误 " + e);

e.printStackTrace();

}

}

}

4、运行结果

读入文件的内容:

Your information:

username: Justme0

email:

realName:

website: blog.csdn.net/justme0

location:

birthday:

aboutMe: Hello!


文件查找

// 习题7.9
import java.io.*;
public class cam1
{
	public static int mount = 0;
    public static void main(String[] args) 
    {
		String filename = "C:\\Users\\Administrator\\Desktop\\code";
		//创建一个 File 实例,表示路径名是指定路径参数的文件
		File file = new File(filename);
		if(args.length>0) //输入要查找的关键字
		{
			findFile(file, args[0]);
			print(args[0]);
		}
	}
    public static boolean isTrueFile(File file) 
    {
		if(!file.exists() || !file.canRead())
		return false;
		if (file.getName().startsWith("."))
		return false;
		if (file.getName().endsWith("."))
	    return false;
		return true;
	}
    public static void findFile(File file, String word) 
    {
		File[] listFiles = file.listFiles(); 
		//得到一个File数组,它默认是按文件最后修改日期排序的
		for (int i = 0; i < listFiles.length; i++) 
		{
		   if (listFiles[i].isDirectory())
		   findFile(listFiles[i], word);
		   else if (isTrueFile(listFiles[i]))
		   search(listFiles[i], word);
		}
	}
    public static void search(File file, String word) 
    {
		try 
		{
			int j = 0, k = 0, ch = 0;
			String str = null;
			FileReader in = new FileReader(file);
			while ((ch = in.read()) != -1) 
			{
				str += (char) ch;
			}
			if (str != null)
			{
				while (str.indexOf(word, j) != -1) 
				{
					k++;
					j = str.indexOf(word, j) + 1; // 返回第一次出现的指定子字符串在此字符串中的索引
				}
			}
			if (k > 0) 
			{
				System.out.println("在" + file.getAbsolutePath() + "有" + k+ "个关键字");
				mount++;
			}
			in.close();
		} 
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
    public static void print(String word)
    {
		if (mount != 0) 
		{
			System.out.println("找到" + mount + "个文本包含关键字" + word + "!");
		} 
		else 
		{
			System.out.println("没有找到相应的文件");
		}
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值