java IO文件操作简单基础入门例子
来源:互联网 发布:微信支付网页授权域名 编辑:程序博客网 时间:2024/05/17 12:55
1. [代码]1、文件拷贝 01try {02 File inputFile = new File(args[0]);03 if (!inputFile.exists()) {04 System.out.println("源文件不存在,程序终止");05 System.exit(1);06 }07 File outputFile = new File(args[1]);08 InputStream in = new FileInputStream(inputFile);09 OutputStream out = new FileOutputStream(outputFile);10 byte date[] = new byte[1024];11 int temp = 0;12 while ((temp = in.read(date)) != -1) {13 out.write(date);14 }15 in.close();16 out.close();17 } catch (FileNotFoundException e) {18 // TODO Auto-generated catch block19 e.printStackTrace();20 } catch (IOException e) {21 // TODO Auto-generated catch block22 e.printStackTrace();23 }2. [代码]2、java读文件:实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数,除此之外没有其他字符 01String fileName = "D:/date.java.bak";02 // String fileName = "D:/test.qqq";03 String line;04 int i = 0, j = 0, f = 0, k = 0;05 try {06 BufferedReader in = new BufferedReader(new FileReader(fileName));07 line = in.readLine();08 while (line != null) {09 // System.out.println(line);10 char c[] = line.toCharArray();11 for (int i1 = 0; i1 < c.length; i1++) {12 // 如果是字母13 if (Character.isLetter(c[i1]))14 i++;15 // 如果是数字16 else if (Character.isDigit(c[i1]))17 j++;18 // 是空格19 else if (Character.isWhitespace(c[i1]))20 f++;21 }22 line = in.readLine();23 k++;24 }25 in.close();26 System.out27 .println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k);28 } catch (IOException e) {29 e.printStackTrace();30 }3. [代码]3、 从文件(d:\test.txt)中查出字符串”aa”出现的次数 01try {02 BufferedReader br = new BufferedReader(new FileReader(03 "D:\\test.txt"));04 StringBuilder sb = new StringBuilder();05 while (true) {06 String str = br.readLine();07 if (str == null)08 break;09 sb.append(str);10 }11 Pattern p = Pattern.compile("aa");12 Matcher m = p.matcher(sb);13 int count = 0;14 while (m.find()) {15 count++;16 }17 System.out.println("\"aa\"一共出现了" + count + "次");18 } catch (FileNotFoundException e) {19 e.printStackTrace();20 } catch (IOException e) {21 e.printStackTrace();22 }4. [代码]4、 三种方法读取文件 01try {02 // 方法一03 BufferedReader br = new BufferedReader(new FileReader(new File(04 "D:\\1.xls")));05 // StringBuilder bd = new StringBuilder();06 StringBuffer bd = new StringBuffer();07 while (true) {08 String str = br.readLine();09 if (str == null) {10 break;11 }12 System.out.println(str);13 bd.append(str);14 }15 br.close();16 // System.out.println(bd.toString());17 // 方法二18 InputStream is = new FileInputStream(new File("d:\\1.xls"));19 byte b[] = new byte[Integer.parseInt(new File("d:\\1.xls").length()20 + "")];21 is.read(b);22 System.out.write(b);23 System.out.println();24 is.close();25 // 方法三26 Reader r = new FileReader(new File("d:\\1.xls"));27 char c[] = new char[(int) new File("d:\\1.xls").length()];28 r.read(c);29 String str = new String(c);30 System.out.print(str);31 r.close();32 } catch (RuntimeException e) {33 // TODO Auto-generated catch block34 e.printStackTrace();35 } catch (FileNotFoundException e) {36 // TODO Auto-generated catch block37 e.printStackTrace();38 } catch (IOException e) {39 // TODO Auto-generated catch block40 e.printStackTrace();41 }5. [代码]5、三种方法写文件 01try {02 PrintWriter pw = new PrintWriter(new FileWriter("d:\\1.txt"));03 BufferedWriter bw = new BufferedWriter(new FileWriter(new File(04 "d:\\1.txt")));05 OutputStream os = new FileOutputStream(new File("d:\\1.txt"));06 // 107 os.write("ffff".getBytes());08 // 209 // bw.write("ddddddddddddddddddddddddd");10 // 311 // pw.print("你好sssssssssssss");12 bw.close();13 pw.close();14 os.close();15 } catch (IOException e) {16 // TODO Auto-generated catch block17 e.printStackTrace();18 }6. [代码]6、读取文件,并把读取的每一行存入double型数组中 view sourceprint?01try {02 BufferedReader br = new BufferedReader(new FileReader(new File(03 "d:\\2.txt")));04 StringBuffer sb = new StringBuffer();05 while (true) {06 String str = br.readLine();07 if (str == null) {08 break;09 }10 sb.append(str + "、");11 }12 String str = sb.toString();13 String s[] = str.split("、");14 double d[] = new double[s.length];15 for (int i = 0; i < s.length; i++) {16 d[i] = Double.parseDouble(s[i]);17 }18 for (int i = 0; i < d.length; i++) {19 System.out.println(d[i]);20 }21 br.close();22 } catch (FileNotFoundException e) {23 // TODO Auto-generated catch block24 e.printStackTrace();25 } catch (IOException e) {26 // TODO Auto-generated catch block27 e.printStackTrace();28 }
0 0