JAVA文本文件增删改查

题目内容

建立一个文本文件来保存数据(每行存储一行数据,一行数据至少包含4个属性),建立一个类,类中包含若干个查询方法,以及增加、删除、修改方法,请将文本文件截图、类文件截图、测试结果截图(每个方法都需要测试)上传(注意为了方便批改,请不要上传源文件)。

源程序及运行结果

package Book;

public class Book 
{
	public String num;
	public String name;
	public String author;
	public String press;
	public String type;
	
	public Book(String num,String name,String author,String press,String type)
	{
		this.num = num;
		this.name = name;
		this.author = author;
		this.press = press;
		this.type = type;
	}

}

package Book;
import java.io.*;
import java.util.*;

public class BookDeal 
{
	public Book findBookByNum(String num)
	{
		Book result=null;
		try
		{
			FileReader fr=new FileReader("e:\\Book.txt");
			BufferedReader br=new BufferedReader(fr);
			String temp=br.readLine();
			while(temp!=null)
			{
				String[]infos=temp.split(",");
				if(infos[0].equals(num))
				{
					result=new Book(infos[0],infos[1],infos[2],infos[3],infos[4]);
					break;
				}
				temp=br.readLine();
			}
			br.close();
			fr.close();
		}
		catch(FileNotFoundException ex)
		{
		}
		catch(IOException ex)
		{	
		}
		return result;
	}

	public Book[] findBooksByNameAt(String name)
	{
		ArrayList<Book> result=new ArrayList<Book>();
		try
		{
			FileReader fr=new FileReader("e:\\Book.txt");
			BufferedReader br=new BufferedReader(fr);
			String temp=br.readLine();
			while(temp!=null)
			{
				String[]infos=temp.split(",");
				if(infos[1].contains(name))
				{
					result.add(new Book(infos[0],infos[1],infos[2],infos[3],infos[4]));
				}
				temp=br.readLine();
			}
			br.close();
			fr.close();
		}
		catch(FileNotFoundException ex)
		{
		}
		catch(IOException ex)
		{	
		}
		return (Book[])result.toArray(new Book[result.size()]);
	}
	
	public Book[] findBookByAuthor(String author)
	{
		ArrayList<Book> result=new ArrayList<Book>();
		try
		{
			FileReader fr=new FileReader("e:\\Book.txt");
			BufferedReader br=new BufferedReader(fr);
			String temp=br.readLine();
			while(temp!=null)
			{
				String[]infos=temp.split(",");
				if(infos[2].equals(author))
				{
					result.add(new Book(infos[0],infos[1],infos[2],infos[3],infos[4]));
				}
				temp=br.readLine();
			}
			br.close();
			fr.close();
		}
		catch(FileNotFoundException ex)
		{
		}
		catch(IOException ex)
		{	
		}
		return (Book[])result.toArray(new Book[result.size()]);
	}
	
	public ArrayList<Book>  findAllBooks()
	{
		ArrayList<Book> result=new ArrayList<Book>();
		try
		{
			FileReader fr=new FileReader("e:\\Book.txt");
			BufferedReader br=new BufferedReader(fr);
			String temp=br.readLine();
			while(temp!=null)
			{
				String[] infos=temp.split(",");
				result.add(new Book(infos[0],infos[1],infos[2],infos[3],infos[4]));
				temp=br.readLine();
			}
			br.close();
			fr.close();
		}
		catch(FileNotFoundException ex)
		{
		}
		catch(IOException ex)
		{	
		}
		return result;
	}
	
	private boolean checkNumIsExist(String num)
	{
		boolean result=false;
		try
		{
			FileReader fr=new FileReader("e:\\Book.txt");
			BufferedReader br=new BufferedReader(fr);
			String temp=br.readLine();
			while(temp!=null)
			{
				String[] infos=temp.split(",");
				if(infos[0].equals(num))
				{
					result=true;
					break;
				}
				temp=br.readLine();
			}
			br.close();
			fr.close();
		}
		catch(FileNotFoundException ex)
		{
		}
		catch(IOException ex)
		{	
		}
		return result;
	}
	
	private boolean checkNameIsExist(String name)
	{
		boolean result=false;
		try
		{
			FileReader fr=new FileReader("e:\\Book.txt");
			BufferedReader br=new BufferedReader(fr);
			String temp=br.readLine();
			while(temp!=null)
			{
				String[] infos=temp.split(",");
				if(infos[1].equals(name))
				{
					result=true;
					break;
				}
				temp=br.readLine();
			}
			br.close();
			fr.close();
		}
		catch(FileNotFoundException ex)
		{
		}
		catch(IOException ex)
		{	
		}
		return result;
	}
	
	public void addBook(Book book)
	{
		if(!checkNumIsExist(book.num))
		{
			try
			{
				FileWriter fw=new FileWriter("e:\\Book.txt",true);
				BufferedWriter bw = new BufferedWriter(fw);
				StringBuffer str=new StringBuffer();
				str.append(book.num+",");
				str.append(book.name+",");
				str.append(book.author+",");
				str.append(book.press+",");
				str.append(book.type);
				bw.newLine();
				bw.write(str.toString());
				bw.close();
				fw.close();
			}
			catch(FileNotFoundException ex)
			{
			}
			catch(IOException ex)
			{	
			}
		}
	}
	
	public void delBookByName(String name)
	{
		if(checkNameIsExist(name))
		{
			ArrayList<Book> books=findAllBooks();
			for(int i=0;i<books.size();i++)
			{
				Book temp=(Book)books.get(i);
				if(temp.name.equals(name))
				{
					books.remove(temp);
					break;
				}
			}
			try {
				FileWriter fw = new FileWriter("e:\\Book.txt");
				BufferedWriter bw = new BufferedWriter(fw);
				for(int i=0;i<books.size();i++)
				{
					Book temp=(Book)books.get(i);
					StringBuffer str=new StringBuffer();
					str.append(temp.num+",");
					str.append(temp.name+",");
					str.append(temp.author+",");
					str.append(temp.press+",");
					str.append(temp.type);
					bw.write(str.toString());
					bw.newLine();
				}
				
				bw.close();
				fw.close();
			} 
			catch(FileNotFoundException ex)
			{
			}
			catch(IOException ex)
			{	
			}
		}
	}
	public void updateBook(Book book)
	{
		delBookByName(book.name);
		addBook(book);
	}
	
	public void updateBooks(Book[] books)
	{
		if(books.length>0)
		{
			for(int i=0;i<books.length;i++)
			{
				updateBook(books[i]);
			}
		}
	}
	
	public void delStus(String[] names)
	{
		if(names.length>0)
		{
			for(int i=0;i<names.length;i++)
			{
				delBookByName(names[i]);
			}
		}
	}
}

package Book;

public class Text {
	public static void main(String[] args) {
		BookDeal deal=new BookDeal();
		
//		Book book1=new Book("211","三体1","刘慈欣","重庆出版社","科幻类");
//		deal.addBook(book1);
//		deal.delBookByName("狂人日记");
//		deal.delBookByName("十宗罪");
		
		Book book=deal.findBookByNum("231");
		System.out.println(book.name);
		Book[] book3=deal.findBooksByNameAt("三");
		for(int i=0;i<book3.length;i++){
			System.out.print(book3[i].num+",");
			System.out.print(book3[i].name+",");
			System.out.print(book3[i].author+",");
			System.out.print(book3[i].press+",");
			System.out.println(book3[i].type);
		}
		Book[] book2=deal.findBookByAuthor("鲁迅");
		for(int i=0;i<book2.length;i++){
			System.out.print(book2[i].num+",");
			System.out.print(book2[i].name+",");
			System.out.print(book2[i].author+",");
			System.out.print(book2[i].press+",");
			System.out.println(book2[i].type);
		}

	}

}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 8
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值