1. /*从键盘输入一个文件名在屏幕显示文件内容*/
    2. import java.util.Scanner;
    3. import java.io.File;
    4. import java.io.*;
    5. public class test13_3 {
    6. public static void Print(File file)
    7. {
    8. try{
    9. BufferedReader bu = new BufferedReader(new FileReader(file));
    10. //构造一个BufferedReader类来读取文件
    11. String s = null;
    12. while((s = bu.readLine())!=null)//readLine方法,一次读一行
    13. System.out.println(s);
    14. bu.close();
    15. }catch(Exception e)
    16. {
    17. e.printStackTrace();//在命令行打印异常信息在程序中出错的位置及原因
    18. }
    19. }
    20. public static void main(String[] args) {
    21. Scanner In = new Scanner(System.in);
    22. System.out.println("请输入文件名:");
    23. String str = In.next();
    24. String Str1 = "D:\\" + str + ".txt";//文件具体目录
    25. File file = new File(Str1);
    26. Print(file);
    27. }
    28. }
    1. import java.util.*;
    2. import java.io.*;
    3. public class test13_4 {
    4. public static void WordCount(String Str) {
    5. String Str1=new String();
    6. for(int i=0;i<Str.length();i++){
    7. if(Str.charAt(i)>='a'&&Str.charAt(i)<='z'||Str.charAt(i)>='A'&&Str.charAt(i)<='Z')
    8. Str1+=Str.charAt(i);
    9. else if(Str.charAt(i-1)>='a'&&Str.charAt(i-1)<='z'||Str.charAt(i-1)>='A'&&Str.charAt(i-1)<='Z')
    10. Str1+=' ';
    11. }
    12. String[] Str2=Str1.split(" ");
    13. int count;
    14. for(int i=0;i<Str2.length;i++) {
    15. System.out.print(Str2[i]+" ");
    16. if((i+1)%10==0)
    17. System.out.println();
    18. }
    19. System.out.println("文章中单词个数为:"+Str2.length);
    20. }
    21. public static void SumCount(File file) throws IOException {
    22. try {
    23. BufferedReader bu=new BufferedReader(new FileReader(file));
    24. String s=null;
    25. String S1="";
    26. while((s=bu.readLine())!=null)
    27. S1=S1+s+" ";
    28. //System.out.println(S1);
    29. bu.close();
    30. WordCount(S1);
    31. } catch (FileNotFoundException e) {
    32. // TODO Auto-generated catch block
    33. e.printStackTrace();
    34. }
    35. }
    36. public static void main(String[] args) throws IOException
    37. {
    38. Scanner In=new Scanner(System.in);
    39. System.out.print("请输入文本文件名:");
    40. String str=In.next();
    41. String str1="D:\\学习代码\\java-learning\\java学习\\第十三章1\\"+str+".txt";
    42. File file=new File(str1);
    43. SumCount(file);
    44. }
    45. }
    1. import java.util.*;
    2. import java.io.*;
    3. //利用随机函数产生20个整数,按小到大的顺序排序后写入文件,然后从文件中读取整数并显示。分别用顺序文件和随机文件的读写方式进行编程测试。
    4. public class test13_5 {
    5. public static void main(String[] args) {
    6. ArrayList<Integer> a = new ArrayList<Integer>();
    7. for (int i = 1; i <= 20; i++)
    8. a.add((int) (Math.random() * 100));
    9. for (int i = 0; i < a.size(); i++)
    10. System.out.print(a.get(i) + " ");
    11. a.sort(null);
    12. System.out.println();
    13. for (int i = 0; i < a.size(); i++)
    14. System.out.print(a.get(i) + " ");
    15. //顺序文件
    16. try {
    17. FileWriter fw = new FileWriter("charset.txt");
    18. for (int i = 0; i < a.size(); i++)
    19. fw.write(Integer.toString(a.get(i)) + " ");//.write输出ASCII英文字符,不能直接输出整形
    20. fw.close();
    21. } catch (IOException e) {
    22. // TODO Auto-generated catch block
    23. e.printStackTrace();
    24. }
    25. System.out.println();
    26. ArrayList<Integer> a1 = new ArrayList<Integer>(); //读取的数据存储在a1中
    27. try {
    28. BufferedReader in = new BufferedReader(new FileReader("charset.txt"));//构造一个BufferedReader类来读取文件
    29. String s = null;
    30. while ((s = in.readLine()) != null)//readLine方法,一次读一行
    31. {
    32. String[] temp = s.split(" ");
    33. for (int j = 0; j < temp.length; j++)
    34. a1.add(0, Integer.valueOf(temp[j]));
    35. }
    36. in.close();
    37. } catch (FileNotFoundException e) {
    38. e.printStackTrace();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. for (int i = 0; i < a.size(); i++)
    43. System.out.print(a1.get(i) + " ");
    44. System.out.println();
    45. //随机文件
    46. StringBuffer sb=new StringBuffer();
    47. for (int i = 0; i < a.size(); i++) {
    48. sb.append(a.get(i));
    49. sb.append(" ");
    50. }
    51. sb.append("\n");
    52. System.out.println(sb);
    53. try {
    54. RandomAccessFile fio = new RandomAccessFile("charset1.txt", "rw");
    55. fio.seek(fio.length()); //移动到文件尾部
    56. fio.writeBytes(sb.toString());
    57. fio.close();
    58. } catch (FileNotFoundException e) {
    59. e.printStackTrace();
    60. } catch (IOException e) {
    61. e.printStackTrace();
    62. }
    63. }
    64. }