本文共 9227 字,大约阅读时间需要 30 分钟。
1、使用Scanner类解析文件。(1)准备文件test.txt内容,内含字符、数字等文本信息;
(2)使用Scanner类读取里面的字符信息和数字信息分别输出。
import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner;public class Main {public static void main(String[] args) throws FileNotFoundException {File file = new File("/tmp/guest-9gs9hd/文档/test.txt");//乌班图系统所以路径特殊Scanner reader = new Scanner(file);StringBuffer b1 = new StringBuffer();StringBuffer b2 = new StringBuffer();String str;while(reader.hasNext()){str = reader.nextLine();for(int i = 0; i <= str.length() - 1; i++){if(str.charAt(i) >= ’0’ && str.charAt(i) <= ’9’){b1.append(str.charAt(i));}else b2.append(str.charAt(i));}System.out.println("文本中的数字信息: " + b1);System.out.println("文本中的字符信息: " + b2);b1.delete(0, b1.length());b2.delete(0, b2.length());}reader.close();}}
2、利用对象输入/输出流进行文件的读写。
根据要求编写程序:
(1)创建类Student,内含学号、姓名、年龄、班级等属性,重写父类的toString()方法,用于输出学生的信息;
(2)建立测试类,建立Student类的对象数组stuArray,里面放置若干个学生对象,将数组中的数据依次写入文件stu.dat;然后读取该文件中的信息,依次还原出数组中各Student对象的信息。
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream; class Student{String id;String name;int age;String Class;public Student(String id, String name, int age, String class1) {super();this.id = id;this.name = name;this.age = age;Class = class1;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + ", Class=" + Class + "]";}} public class Main{public static void main(String [] args) throws FileNotFoundException, IOException, ClassNotFoundException{Student stu1 = new Student("001", "bo", 20, "ruanjian1704");Student stu2 = new Student("002", "wen", 20, "ruanjian1705");Student stuArray[] = new Student[] {stu1, stu2};ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("/tmp/guest-9gs9hd/文档/stu.dat"));ObjectInputStream is = new ObjectInputStream(new FileInputStream("/tmp/guest-9gs9hd/文档/stu.dat"));os.writeObject(stuArray);Object object = is.readObject();if(object instanceof Student[]){Student [] stu = (Student[])object;for(Student id : stu){System.out.println(id);}}os.close();is.close();}}
3、用打开文本对话框选取文件,读写文件内容。
用打开文本对话框选取文件,读取文件内容,在显示器中显示出来,进行小写字母转换成大写字母,然后用保存文本对话框选取目标文件,向目标文件里面追加写入处理后的字符。
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException; import javax.swing.JFileChooser; public class Main {public static void main(String[] args) throws IOException {StringBuffer b = new StringBuffer();JFileChooser chooser = new JFileChooser();if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){File file = chooser.getSelectedFile();BufferedReader br = new BufferedReader(new FileReader(file));String line;while((line = br.readLine()) != null){line = line.toUpperCase();b.append(line);}System.out.println(b);br.close();}if(chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){File file = chooser.getSelectedFile();BufferedWriter bw = new BufferedWriter(new FileWriter(file));bw.write(b.toString());bw.flush();bw.close();}} }
4、读取网络资源,写出代码至文件。
读取“http://www.sdut.edu.cn"网页内容,存至“sdut.html"。
import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.URL; public class Main{public static void main(String [] args) throws IOException{URL url = new URL("http://www.sdut.edu.cn");InputStream in = url.openStream();OutputStream os = new FileOutputStream("/tmp/guest-3fqtkq/文档/sdut.html");//乌班图系统比较特殊byte [] bytes = new byte[10];int len;while((len = in.read(bytes)) != -1){os.write(bytes, 0, len);}}}
5、利用各种输入输出流进行读写文件,分析其读写效率。
读出文件D:\test1.txt的内容,显示在屏幕的同时,将文件写至D:\test2.txt中,计算其执行的时间,从而分析执行的效率
Import java.io.*;public class Main{public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("c:/java/test1.txt");FileOutputStream fos = new FileOutputStream("c:/java/test2.txt");long t1 = System.currentTimeMillis();byte[] b1 = new byte[1024];int len = 0;while((len = fis.read(b1))!=-1) {String str1 = new String(b1);System.out.print(str1);fos.write(b1, 0, len);}System.out.println();fis.close();fos.flush();fos.close();long t2 = System.currentTimeMillis();System.out.println(t2-t1);System.out.println("--------------------");BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c:/java/test1.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:/java/test2.txt"));long t3 = System.currentTimeMillis();byte[] b2 =new byte[1024];int len2 = 0;while((len2=bis.read(b2))!=-1) {String str2 = new String(b2);System.out.print(str2);bos.write(b2,0,len2);}System.out.println();bis.close();bos.flush();bos.close();long t4 = System.currentTimeMillis();System.out.println(t4-t3);System.out.println("--------------------");DataInputStream dis = new DataInputStream(new FileInputStream("c:/java/test1.txt"));DataOutputStream dos = new DataOutputStream(new FileOutputStream("c:/java/test2.txt"));long t5 = System.currentTimeMillis();byte[] b3 = new byte[1024];for(int len3 = 0;while((len3=dis.read(b3))!=-1) {String str3 = new String(b3);System.out.print(str3);dos.write(b3, 0, len3);}System.out.println();dis.close();dos.flush();dos.close();long t6 = System.currentTimeMillis();System.out.println(t6-t5);System.out.println("--------------------");FileReader fr = new FileReader("c:/java/test1.txt");FileWriter fw = new FileWriter("c:/java/test2.txt");long t7 = System.currentTimeMillis();char[] c1 = new char[1024];int len4 = 0;while((len4=fr.read(c1))!=-1) {String str4 = new String(c1);System.out.print(str4);fw.write(c1, 0, len4);}System.out.println();fr.close();fw.flush();fw.close();long t8 = System.currentTimeMillis();System.out.println(t8-t7);System.out.println("--------------------");BufferedReader br = new BufferedReader(new FileReader("c:/java/test1.txt"););BufferedWriter bw = new BufferedWriter(new FileWriter("c:/java/test2.txt"););long t9 = System.currentTimeMillis();char[] c2 = new char[1024];int len5 = 0;while((len5=br.read(c2))!=-1) {String str5 = new String(c2);System.out.print(str5);bw.write(c2, 0, len5);}System.out.println();br.close();bw.flush();bw.close();long t10 = System.currentTimeMillis();System.out.println(t10-t9);}}
6、利用DataInputStream和DataOutputStream读写文件。
将数字1-100、字母A-Z、a—z写出至文件D:\abc.txt中,然后读取出来,显示在屏幕上。
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; public class Main{public static void main(String [] args) throws IOException{FileOutputStream output = new FileOutputStream("/tmp/guest-3fqtkq/文档/test1.txt");//乌班图系统所以比较特殊DataOutputStream da = new DataOutputStream(output); FileInputStream input = new FileInputStream("/tmp/guest-3fqtkq/文档/test1.txt");//乌班图系统所以比较特殊DataInputStream di = new DataInputStream(input);for(int i = 1; i <= 100; i++){da.writeInt(i);}for(int i = 65; i <= 90; i++){da.writeChar(i);}for(int i = 97; i <= 122; i++){da.writeChar(i);}da.close();for(int i = 1; i <= 100 - 1; i++){System.out.print(di.readInt() + " ");}System.out.println(di.readInt());for(int i = 65; i <= 90 - 1; i++){System.out.print(di.readChar());}System.out.println(di.readChar());for(int i = 97; i <= 122 - 1; i++){System.out.print(di.readChar());}System.out.println(di.readChar());di.close();}}
7、利用File类进行文件及文件夹名称的显示。
编写应用程序,列出指定目录(d:/test)下的所有文件和目录名,然后将该目录下的所有文件后缀名为.txt的文件过滤出来显示在屏幕上。
import java.io.File;import java.io.FilenameFilter;import java.util.Scanner; public class Main{public static void main(String [] args){File file = new File("/tmp/guest-3fqtkq");//乌班图系统所以比较特殊if(file.isFile()){System.out.println("file" + " " + file);}else {listFile(file);}File file1 = new File("/tmp/guest-3fqtkq");//乌班图系统所以比较特殊String [] listfilenames = file1.list(new FilenameFilter(){ @Overridepublic boolean accept(File dir, String name) {// TODO 自动生成的方法存根if(name.endsWith(".txt")){return true;}else return false;}});for(String filename : listfilenames){System.out.println(filename);}} private static void listFile(File file) {// TODO 自动生成的方法存根 if(file != null){File[] listFiles = file.listFiles();if(listFiles != null){for(File filetmp : listFiles){if(filetmp.isDirectory()){System.out.println("direction" + " " + filetmp);listFile(filetmp);}else {System.out.println(filetmp);} }} }}}
8、使用File类创建一个多层目录d:\java\ch10\src。
package hello;import java.io.File;public class Main{public static void main(String [] args){File file = new File("/tmp/guest-3fqtkq/k");//乌班图系统所以比较特殊if(!file.exists()){file.mkdirs();}else file.delete();}}
9、使用File类列出某一个目录下创建日期晚于2017-08-10的文件。
import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.nio.file.attribute.BasicFileAttributes;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.*;public class Main {public static void main(String[] args) throws ParseException {File file = new File("("c:/java/test1.txt");");File files[] = file.listFiles();String str = "2017/08/10";SimpleDateFormat pattern = new SimpleDateFormat("yyyy/MM/dd");Date bdate = pattern.parse(str);for(int i=0; i
转载地址:http://onwtb.baihongyu.com/