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
    评论
A: 思路: 1. 创建一个文本文件,用于存储记事本中记录的内容。 2. 设计菜单,允许用户选择要执行的操作。 3. 根据用户输入的操作选择,执行相应的方法。 4. 编写增删改查的具体逻辑。 代码实现: ```java import java.util.Scanner; import java.io.*; public class NotePad { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String fileName = "note.txt"; while (true) { //1.显示菜单 System.out.println("Menu: "); System.out.println("1.添加记录"); System.out.println("2.删除记录"); System.out.println("3.修改记录"); System.out.println("4.查找记录"); System.out.println("5.退出"); //2.让用户选择 System.out.print("请输入要执行的操作:"); int choice = sc.nextInt(); switch (choice) { case 1: addRecord(fileName); break; case 2: deleteRecord(fileName); break; case 3: modifyRecord(fileName); break; case 4: searchRecord(fileName); break; case 5: System.exit(0);//退出程序 default: System.out.println("输入有误,请重新输入!"); break; } } } //添加记录 public static void addRecord(String fileName) { Scanner sc = new Scanner(System.in); BufferedWriter bw = null; try { File file = new File(fileName); bw = new BufferedWriter(new FileWriter(file, true)); System.out.print("请输入要添加的内容:"); String content = sc.nextLine(); bw.write(content); bw.newLine();//换行 bw.flush(); System.out.println("添加成功!"); } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } //删除记录 public static void deleteRecord(String fileName) { Scanner sc = new Scanner(System.in); BufferedReader br = null; BufferedWriter bw = null; try { File file = new File(fileName); File tempFile = new File("temp.txt"); br = new BufferedReader(new FileReader(file)); bw = new BufferedWriter(new FileWriter(tempFile)); System.out.print("请输入要删除的内容:"); String content = sc.nextLine(); String line; while ((line = br.readLine()) != null) { if (line.equals(content)) { System.out.println("删除成功!"); continue; } bw.write(line); bw.newLine(); } file.delete(); tempFile.renameTo(file);//重命名 } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } //修改记录 public static void modifyRecord(String fileName) { Scanner sc = new Scanner(System.in); BufferedReader br = null; BufferedWriter bw = null; try { File file = new File(fileName); File tempFile = new File("temp.txt"); br = new BufferedReader(new FileReader(file)); bw = new BufferedWriter(new FileWriter(tempFile)); System.out.print("请输入要修改的内容:"); String oldContent = sc.nextLine(); System.out.print("请输入新内容:"); String newContent = sc.nextLine(); String line; while ((line = br.readLine()) != null) { if (line.equals(oldContent)) { bw.write(newContent); } else { bw.write(line); } bw.newLine(); } file.delete(); tempFile.renameTo(file);//重命名 System.out.println("修改成功!"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (bw != null) { bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } //查找记录 public static void searchRecord(String fileName) { Scanner sc = new Scanner(System.in); BufferedReader br = null; try { File file = new File(fileName); br = new BufferedReader(new FileReader(file)); System.out.print("请输入要查找的内容:"); String content = sc.nextLine(); String line; boolean flag = false; while ((line = br.readLine()) != null) { if (line.equals(content)) { System.out.println("找到了这条记录:" + line); flag = true; } } if (flag == false) { System.out.println("没有找到这条记录!"); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } } catch (IOException e) { e.printStackTrace(); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值