1. 编写一个程序,从键盘读入一串英文字符串,将每个英文单词的首字母转换成大写后存 到 D:\word.txt 文件中。

实验九 Java 语言的输入输出与文件处理
实验目的
1.了解流式输入输出的基本原理。
2.掌握 File、FileInputStream、FileOutputStream 类的使用方法。
3.掌握 FileReader、FileWriter、BufferedReader 类的使用方法。
主要仪器设备及耗材
安装了 JDK1.8 的 PC 一台
实验内容
1. 编写一个程序,从键盘读入一串英文字符串,将每个英文单词的首字母转换成大写后存
到 D:\word.txt 文件中。
2. 编写一个程序,将一个文本文件的内容按行读出,每读出一行就顺序加上行号,并写入
到另一个文件中。

 

package com.temp;

import java.io.*;
import java.util.StringTokenizer;

/**
 * @Author lanxiaofang
 * @email 983770299@qq.com
 * @date 2020/11/9 8:39
 */
public class InputOutputAndFileHandle {

    private final static String url_1 = "D://word1.txt";
    private final static String url_2 = "D://word2.txt";
    private final static String url_3 = "D://word3.txt";
    private final static String url_4 = "D://word4.txt";

    public static void main(String[] args) {

        //推荐使用BufferedReader,BufferedReader的效率比Scanner 高
        //BufferedReader 是先把数据读到缓存区然后在写到硬盘里,Scanner是直接往硬盘些数据
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("-- 请输入一段英文句子");
        String content = null;
        try {
            content = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
//      Scanner in = new Scanner(System.in);
//      String content = in.nextLine();

        String iNITCAP = iNITCAP(content);

        //使用一
        System.out.println("----使用一");
        useFilWriter(iNITCAP, url_1);
        useFileReader(url_1);
        //使用二
        System.out.println("----使用二");
        useBufferedReader(url_1);
        useBufferedWriter(url_1, url_2);
        useBufferedWriterOfContent(iNITCAP, url_3);
        //使用三
        System.out.println("----使用三");
        useFileOutputStreamAndUseFileInputStream(url_4);

    }

    /**
     * useFileOutputStreamAndUseFileInputStream
     *
     * @param url
     */
    public static int useFileOutputStreamAndUseFileInputStream(String url) {
        //把字节输入流的内容通过字节输出流存到硬盘
        try {
            char ch;
            FileInputStream fileInputStream = new FileInputStream(FileDescriptor.in); //声明文件字节流输入流对象,创建标准输入流对象
            FileOutputStream fileOutputStream = new FileOutputStream(url); // 声明文件字节输出流对象,创建输出流对象为url1中的文件
            //输入一串字符串,以'#'作为结束标准
            while ((ch = (char) fileInputStream.read()) != '#') { // 调用 fileInputStream.read() 从键盘上读取数据
                fileOutputStream.write(ch); //fileOutputStream.write(ch)从键盘上读取的数据写入到url1中的文件中
            }
            System.out.println("-- fileOutputStream -- Write is finished");
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        //从硬盘通过字节输入流读取数据,字节输出流输出数据到控制台
        try {
            int data;
            FileInputStream fIStream = new FileInputStream(url);
            FileOutputStream fOStream = new FileOutputStream(FileDescriptor.out);
            while (fIStream.available() > 0) {
                data = fIStream.read();
                fOStream.write(data);
            }
            System.out.println("-- fOStream -- Write is finished");
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    /**
     * useFilWriter
     *
     * @param content,url Writes the content to the URL file and prompts it for success.
     */
    public static int useFilWriter(String content, String url) {
        try {
            FileWriter fileWriter = new FileWriter(url);
            fileWriter.write(content);
            fileWriter.close();
            System.out.println("--useFilWriter-- writer to " + url + " success.");
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    /**
     * useFileReader
     *
     * @param url Read the content from the url file and output it.
     */
    public static int useFileReader(String url) {
        try {
            char[] content = new char[1024];
            FileReader fileReader = new FileReader(url);
            int num = fileReader.read(content);
            String reContent = new String(content, 0, num);
            System.out.println("--useFileReader-- had reade " + num + " chars :\n" + reContent);
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    /**
     * useBufferedReader
     *
     * @param url Read content from the URL file, read it line by line,
     *            output it line by line, and count the total number of lines read.
     */
    public static int useBufferedReader(String url) {
        String thisLine;
        int count = 0;
        try {
            FileReader fileReader = new FileReader(url);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            while ((thisLine = bufferedReader.readLine()) != null) {
                count++;
                System.out.println(thisLine);
            }
            System.out.println("--useBufferedReader-- had read " + count + " lines");
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    /**
     * useBufferedWriter
     *
     * @param from_url,to_url Copy the contents from the file in path from_url to the file in path to_url
     *                        Read a line, copy a line, and then create a new line
     */
    public static int useBufferedWriter(String from_url, String to_url) {
        String thisLine;
        int count = 0;
        try {
            FileReader fileReader = new FileReader(from_url);
            FileWriter fileWriter = new FileWriter(to_url);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            while ((thisLine = bufferedReader.readLine()) != null) {
                count++;
                System.out.println(thisLine);
                bufferedWriter.write(thisLine);
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
            System.out.println("--useBufferedWriter-- had read " + count + " lines. \n--useBufferedWriter-- had writer +count+\" lines.");
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    /**
     * useBufferedWriterOfContent
     *
     * @param content,to_url writes the content to the file at the to_url
     */
    public static int useBufferedWriterOfContent(String content, String to_url) {
        try {
            FileWriter fileWriter = new FileWriter(to_url);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            bufferedWriter.flush();
            System.out.println("--useBufferedWriter-- had writer success.");
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }


    /**
     * iNITCAP
     * Constructs a iNITCAP function with the specified detail content.
     * You can use s1.toString().trim() or s1.toString().substring(0,s1.length()-1)
     * to clear the space at the end of the string
     *
     * @param content the detail content (which is saved for later convert
     *                the first letter of each English word to uppercase
     */
    public static String iNITCAP(String content) {
        StringBuffer s1 = new StringBuffer();
        StringTokenizer st = new StringTokenizer(content);
        while (st.hasMoreTokens()) {
            String str = st.nextToken();
            char c = (str).charAt(0);
            if (c >= 'a' && c <= 'z') {
                c = (char) ((int) c - 32);
                s1.append(c);
            } else
                s1.append(c);
            for (int i = 1; i < (str).length(); i++) {
                char c1 = (str).charAt(i);
                s1.append(c1);
            }
            s1.append(" ");
        }
        System.out.println("--iNITCAP--" + s1.toString().trim());
        return s1.toString();
    }


}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒笑翻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值