欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

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

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

 

package com.temp;

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

/**
 * @Author lanxiaofang
 * @email aaa@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. 编写一个程序,从键盘读入一串英文字符串,将每个英文单词的首字母转换成大写后存 到 D:\word.txt 文件中。

 

相关标签: Java java