Java笔记——字符文本操作:file、filewriter、filereader

来源:互联网 发布:sql not like 编辑:程序博客网 时间:2024/05/01 10:32
package tmp;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Scanner;//知识点:1、字符串容器stringbuilder的常用方法有append、delete、insert、tostring//知识点:2、File类的常用方法:先建一个文件 new File("path"),然后判断是否存在此文件exists,创建文件createnewfile(),delete()删除文件//知识点:3、filewriter常用方法:write(String str)//知识点:4、filereader常用方法:read(),返回一个整数,当为-1时结束,取值方法if((ch=file.read())!=-1){(char)ch}public class Input_Output_test {public static void main(String[] args) throws Exception {Input_Output_test x=new Input_Output_test();System.out.println("                                                            这是一个员工信息表 ");while(1==1){System.out.print('\n'+"增加员工信息输入数字1,查看信息表记录输入数字2,删除记录表输入数字3,输入其它字符将推出程序: ");@SuppressWarnings("resource")Scanner in=new Scanner(System.in);int intext=in.nextInt();if(intext==1){x.input();}else if(intext==2){x.output();}else{x.delete(intext);}}}public void output() throws IOException {FileReader file=new FileReader("F:\\JAVA_员工信息表.txt");int ch;while((ch=file.read())!=-1){System.out.print((char)ch);}file.close();}public void input() throws Exception{File file=new File("F:","JAVA_员工信息表.txt");if(!file.exists()){System.out.println('\n'+"提示信息:创建文件"+file.createNewFile()+'\n');}StringBuilder str=new StringBuilder();System.out.print("请输入员工名字: ");@SuppressWarnings("resource")Scanner txt=new Scanner(System.in);str.append(txt.next());System.out.print("请输入员工ID: ");str.append(" "+txt.next()+"\r\n");FileWriter file1=new FileWriter("F:\\JAVA_员工信息表.txt",true);file1.write(str.toString());file1.close();}public void delete(int i) throws Exception{File file=new File("F:","JAVA_员工信息表.txt");if(i==3){   if(file.exists()){   file.delete();   System.out.println("提示信息:文件删除成功!");   }   else{   System.out.println("提示信息:文件本来就不存在!");   }}else{throw new Exception("提示信息:输入无效字符,程序不在运行 ");}}}

0 0
原创粉丝点击