1. package demo13;
    2. import java.io.*;
    3. import java.util.*;
    4. public class text1 {
    5. String dir="d:\\mytext.txt";
    6. int max=-1;
    7. public void seta() {
    8. Random rd=new Random();
    9. try{
    10. PrintWriter out=new PrintWriter(dir);
    11. for(int i=0;i<=4;i++) {
    12. out.println(rd.nextInt(9));
    13. }
    14. out.close();}catch(Exception e) {
    15. System.out.println(e);
    16. }
    17. }
    18. public void js() {
    19. File myf=new File(dir);
    20. String x;
    21. int xint;
    22. try {
    23. if (myf.exists()){
    24. Scanner in=new Scanner(myf);
    25. while(in.hasNextLine()){
    26. x=in.nextLine();
    27. System.out.println(x);
    28. xint=Integer.parseInt(x);
    29. if(max<xint) { max=xint; }
    30. }
    31. in.close();}
    32. }catch(Exception e){
    33. System.out.println(e); }
    34. }
    35. public static void main(String[] args) {
    36. text1 my=new text1();
    37. my.seta();
    38. my.js();
    39. System.out.println("................");
    40. System.out.println(my.max);
    41. }
    42. }
    1. package demo13;
    2. //实例:输入一个文件名并显示该文件的相关属性
    3. import java.io.*;
    4. public class l3 {
    5. public static void main(String[] args) {
    6. try{
    7. File f=new File("d:\\myfile1.txt");
    8. if(f.exists()){
    9. System.out.println("文件是否可读"+f.canRead());
    10. System.out.println("文件名:"+f.getName());
    11. System.out.println("文件路径"+f.getPath());
    12. System.out.println("文件绝对路径"+f.getAbsolutePath());
    13. System.out.println("文件长度"+f.length());
    14. }else{
    15. System.out.println("文件没有找到");
    16. }
    17. }catch(Exception e){
    18. System.out.print(e);
    19. }
    20. }
    21. }
    1. package demo13;
    2. import java.io.*;
    3. public class l4 {
    4. public void filewrite(String filename) {
    5. try {
    6. FileInputStream fin=new FileInputStream(FileDescriptor.in);
    7. FileOutputStream fout=new FileOutputStream(filename);
    8. System.out.println("以#字号结束");
    9. char ch;
    10. while((ch=(char)fin.read())!='#'){
    11. fout.write(ch);
    12. }
    13. fin.close();
    14. fout.close(); }catch(Exception e){
    15. System.out.print(e);
    16. }
    17. }
    18. public void fileread(String filename) {
    19. try {
    20. FileInputStream fin=new FileInputStream(filename);
    21. FileOutputStream fout=new FileOutputStream(FileDescriptor.out);
    22. while(fin.available()>0){
    23. int data=fin.read();
    24. fout.write(data);
    25. }
    26. fin.close();
    27. fout.close(); }catch(Exception e) {
    28. System.out.print(e);
    29. }
    30. }
    31. public static void main(String[] args) {
    32. l4 my=new l4();
    33. my.filewrite("d:\\myfile.txt");
    34. my.fileread("d:\\myfile.txt");
    35. }
    36. }
    1. package demo13;
    2. import java.io.*;
    3. import java.util.*;
    4. public class l6 {
    5. public static void main(String[] args) {
    6. String name="有趣而简单的写操作";
    7. String name1="即文本的输出";
    8. double x=3445.4;
    9. String dir="d:\\myfile1.txt";
    10. String list;
    11. //--------------------------------------------------------------------------------------------------------
    12. try{
    13. PrintWriter out=new PrintWriter(dir);
    14. out.print(name+'\n');
    15. out.println(name1);
    16. out.println(" *************** ");
    17. out.println(x);
    18. out.close();
    19. File my1=new File(dir);
    20. if (my1.exists()){
    21. Scanner in=new Scanner(my1);
    22. while(in.hasNextLine()){
    23. System.out.println(in.nextLine());
    24. }
    25. in.close(); }
    26. }
    27. catch(Exception e){
    28. System.out.println(e); }
    29. }
    30. }
    1. package demo13;
    2. import java.io.*;
    3. public class l5 {
    4. public void fileread(String s) {
    5. String thisline;
    6. int count=0;
    7. try{
    8. FileReader fr=new FileReader(s);
    9. BufferedReader bfr=new BufferedReader(fr);
    10. while((thisline=bfr.readLine())!=null){
    11. count++;
    12. System.out.println(thisline);
    13. }
    14. System.out.println("共读取了"+count+"行");
    15. bfr.close();
    16. }catch(IOException e){
    17. System.out.println(e);
    18. }
    19. }
    20. public void filewrite(String sn){
    21. String str=new String();
    22. try{
    23. String s="hello \n java!";
    24. BufferedWriter out=new BufferedWriter(new FileWriter(sn));
    25. out.write(s);
    26. out.close();
    27. }catch(IOException e){
    28. System.out.println(e);
    29. }
    30. }
    31. public static void main(String[] args) {
    32. l5 my=new l5();
    33. my.fileread("d:\\myfile.txt");
    34. my.filewrite("d:\\myfile1.txt");
    35. my.fileread("d:\\myfile1.txt");
    36. } }