zoukankan      html  css  js  c++  java
  • java输入输出流(内容练习)

    1,编写一个程序,读取文件test.txt的内容并在控制台输出。如果源文件不存在,则显示相应的错误信息。

     1 package src;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileReader;
     6 import java.io.IOException;
     7 
     8 public class Test {
     9 
    10     public static void main(String[] args) {
    11         File f = new File("src\test1.txt");//文件在src名为test1.txt中
    12         try {
    13             FileReader fr = new FileReader(f);//将文件读取到内容中
    14             int m;
    15             while((m=fr.read())!=-1){
    16                 System.out.print((char)(m));
    17             }
    18         } catch (FileNotFoundException e) {
    19             // TODO Auto-generated catch block
    20             e.printStackTrace();
    21         } catch (IOException e) {
    22             // TODO Auto-generated catch block
    23             e.printStackTrace();
    24         }
    25     }
    26 }

    2,编写一个程序实现如下功能,从当前目录下的文件fin.txt中读取80个字节(实际读到的字节数可能比80少)并将读来的字节写入当前目录下的文件fout.txt中

     1 package src;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 
     9 public class Test {
    10 
    11     public static void main(String[] args) {
    12         File f1 = new File("src\fin.txt");//src下fin.txt文件
    13         File f2 = new File("src\fout.txt");//src下fout.txt文件
    14         
    15         try {
    16             FileInputStream fis = new FileInputStream(f1);
    17             FileOutputStream fos = new FileOutputStream(f2);
    18             
    19             byte[] temp = new byte[80];//定义一个字节数组
    20             fis.read(temp);//读到内存中
    21             fos.write(temp);//写到文件
    22             
    23             fis.close();
    24             fos.close();
    25         } catch (FileNotFoundException e) {
    26             // TODO Auto-generated catch block
    27             e.printStackTrace();
    28         } catch (IOException e) {
    29             // TODO Auto-generated catch block
    30             e.printStackTrace();
    31         }
    32         
    33     }
    34 }

    3,使用java的输入/输出流技术将一个文本文件的内容按行读出,每读出一行就顺序添加行号,并写入到另一个文件中。

     1 package src;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.BufferedWriter;
     5 import java.io.File;
     6 import java.io.FileNotFoundException;
     7 import java.io.FileReader;
     8 import java.io.FileWriter;
     9 import java.io.IOException;
    10 
    11 public class Test {
    12 
    13     public static void main(String[] args) {
    14         File f1 = new File("src\fin.txt");//src下fin.txt文件
    15         File f2 = new File("src\fout.txt");//src下fout.txt文件
    16         
    17         try {
    18             FileReader fr = new FileReader(f1);
    19             FileWriter fw = new FileWriter(f2);
    20             
    21             BufferedReader br = new BufferedReader(fr);
    22             BufferedWriter bw = new BufferedWriter(fw);
    23             
    24             String temp;
    25             int i=1;
    26             while((temp=br.readLine())!=null){
    27                 bw.write(i+","+temp);
    28                 bw.newLine();//换行
    29                 i++;
    30             }
    31             bw.flush();//把缓冲区内容写到文件
    32             br.close();
    33             bw.close();
    34             br.close();
    35             bw.close();
    36         } catch (FileNotFoundException e) {
    37             // TODO Auto-generated catch block
    38             e.printStackTrace();
    39         } catch (IOException e) {
    40             // TODO Auto-generated catch block
    41             e.printStackTrace();
    42         }
    43     }
    44 }

    4,编写一个程序,接收从键盘输入的数据,并把从键盘输入的内容写到input.txt文件中,如果输入"quit",则程序结束。

     1 package src;
     2 
     3 import java.io.File;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 import java.util.Scanner;
     7 
     8 public class Test {
     9 
    10     public static void main(String[] args) {
    11         File f = new File("src\input.txt");
    12         try {
    13             FileWriter fw = new FileWriter(f);
    14             Scanner scanner = new Scanner(System.in);
    15             String temp;
    16             while(!((temp=scanner.nextLine()).equals("quit"))){
    17                 fw.write(temp);
    18             }
    19             fw.close();
    20         } catch (IOException e) {
    21             // TODO Auto-generated catch block
    22             e.printStackTrace();
    23         }
    24     }
    25 }

    5,编写一个程序实现如下功能,文件fin.txt是无行结构(无换行符)的汉语文件,从fin中读取字符,写入文件fou.txt中,每40个字符一行(最后一行可能少于40个字)

     1 package src;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileReader;
     6 import java.io.FileWriter;
     7 import java.io.IOException;
     8 public class Test {
     9 
    10     public static void main(String[] args) {
    11         File f1=new File("src\fin.txt");
    12         File f2=new File("src\fout.txt");
    13         try {
    14             FileReader fr=new FileReader(f1);
    15             FileWriter fw=new FileWriter(f2);
    16             
    17             char temp[]=new char[40];
    18             int len;
    19             while((len=fr.read(temp))!=-1)
    20             {
    21                 if(len==40)
    22                   fw.write(new String(temp)+"
    ");
    23                 else
    24                     fw.write(temp, 0, len);
    25             }
    26             fr.close();
    27             fw.close();
    28             
    29         } catch (FileNotFoundException e) {
    30             // TODO 自动生成的 catch 块
    31             e.printStackTrace();
    32         } catch (IOException e) {
    33             // TODO 自动生成的 catch 块
    34             e.printStackTrace();
    35         }
    36     }
    37 }
  • 相关阅读:
    Java基础学习总结(41)——JPA常用注解
    Java基础学习总结(41)——JPA常用注解
    【云速建站】视频播放专题
    一招教你如何修复MySQL slave中继日志损坏问题
    【nodejs原理&源码赏析(3)】欣赏手术级的原型链加工艺术
    【云速建站】后台数据批量导入导出
    【云速建站】会员注册弹窗添加及设置
    【nodejs原理&源码赏析(2)】KOA中间件的基本运作原理
    【nodejs原理&源码赏析(1)】Express中间件系统的基本实现
    补习系列(5)-springboot- restful应用
  • 原文地址:https://www.cnblogs.com/wishwzp/p/4834049.html
Copyright © 2011-2022 走看看