娄底手机网站制作/网站的推广方法有哪些
题目1
1、在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去;之后遍历IOTest这个目录下的文件
@Testpublic void test1() throws Exception{File f = new File("E:\\testNomal\\FileTest\\三国1\\HelloWorld.txt");if(f.isFile()){System.out.println("这是一个文件");}else if(f.isDirectory()){System.out.println("这是一个文件夹");}File f1 = new File("E:\\testNomal\\FileTest\\三国1\\IOTest");if(f1.mkdir()){//以字节为单位读取内容FileInputStream fis = new FileInputStream("E:\\testNomal\\FileTest\\三国1\\HelloWorld.txt");//以字节为单位向文件中写入内容FileOutputStream fos = new FileOutputStream("E:\\testNomal\\FileTest\\三国1\\IOTest\\HelloWorld.txt");int i = 0;while((i=fis.read()) != -1){fos.write(i);}fis.close();fos.close();System.out.println("遍历IOTest这个目录下的文件");File[] files = f1.listFiles();for(File fi : files){System.out.println(fi);}}else{System.out.println("文件夹没有创建成功");}
题目2
1、统计一个文件中各个字母出现次数,包括字符出现次数
@Testpublic void test2() throws Exception{//以字节为单位读取内容FileInputStream fis = new FileInputStream("E:\\testNomal\\FileTest\\三国1\\HelloWorld.txt");Map<Character,Integer> s = new LinkedHashMap<Character,Integer>();int i = 0;String sum = "";while((i=fis.read()) != -1){if(s.containsKey((char)i)){Integer c = s.remove((char)i);s.put((char)i, c+1);}else{s.put((char)i, 1);}sum += (char)i;}System.out.println("文档中的字符串为:"+sum);Set<Entry<Character,Integer>> entrySet = s.entrySet();for(Entry<Character,Integer> e : entrySet){Character key = e.getKey();Integer value = e.getValue();System.out.println("字符"+key+"出现的次数为:"+value);}fis.close();}
题目3
1、将一个文本文件内容倒置读出
@Testpublic void test3() throws Exception{//以字节为单位读取内容FileInputStream fis = new FileInputStream("E:\\testNomal\\FileTest\\三国1\\HelloWorld.txt");int i = 0;String sum = "";while((i=fis.read()) != -1){sum += (char)i;}System.out.println("文档中的字符串为:"+sum);StringBuilder s = new StringBuilder(sum);System.out.println("文本文件内容倒置读出为"+s.reverse());fis.close();}
题目4
1、模拟用户登陆,控制台输入用户名和密码,将用户名密码存储到文件中,并随时可以查询所有用户名及密码,在模拟用户登录,读取文件用户名和密码
@Testpublic void test4() throws Exception{//以字节为单位向文件中写入内容FileOutputStream fos = new FileOutputStream("E:\\testNomal\\FileTest\\三国1\\UserPassword.txt");//以字节为单位读取内容FileInputStream fis = new FileInputStream("E:\\testNomal\\FileTest\\三国1\\UserPassword.txt");Scanner sc = new Scanner(System.in);System.out.println("请输入用户名:");String s1 = sc.next();fos.write((s1+"\t").getBytes());System.out.println("请输入密码:");String s2 = sc.next();fos.write(s2.getBytes());int i = 0;String sum = "";while((i=fis.read()) != -1){sum += (char)i;}System.out.println("文档内容为:"+sum);String[] split = sum.split("\t");System.out.println("请输入用户名:");String s3 = sc.next();System.out.println("请输入密码:");String s4 = sc.next();for(int j=0;j<split.length;j++){if(s3.equals(split[j])){if(s4.equals(split[j+1])){System.out.println("用户名和密码正确!");}else{System.out.println("用户名正确,密码错误");}break;}else{System.out.println("用户名密码错误");}}fos.close();fis.close();}
题目5
1、从控制台输入一个文件名,判断所在文件夹是否存在该文件,如果不存在,则创建该文件,如果存在,创建一个copy_文件名的文件
@Testpublic void test5() throws Exception{Scanner sc = new Scanner(System.in);System.out.println("请输入文件名:");String n = sc.next();File f = new File("E:\\testNomal\\FileTest\\三国1\\"+n);if(f.createNewFile()){System.out.println("文件创建成功!");}else{System.out.println(n+"该文件已存在");File f1 = new File("E:\\testNomal\\FileTest\\三国1\\copy_"+n);f1.createNewFile();System.out.println("copy_"+n+"文件创建成功");}}
题目6
1、在当前目录下创建一个文件“test.txt”,并向文件输出“Hello World”,如果文件已存在,则在原有文件内容后面追加。
@Testpublic void test6() throws Exception{Scanner sc = new Scanner(System.in);System.out.println("请输入文件名:");String n = sc.next();File f = new File("E:\\testNomal\\FileTest\\三国1\\"+n);if(f.createNewFile()){System.out.println("文件创建成功!");}else{System.out.println(n+"该文件已存在");//以字节为单位向文件中写入内容FileOutputStream fos = new FileOutputStream("E:\\testNomal\\FileTest\\三国1\\"+n,true);fos.write("Hello World".getBytes());}}