编写一个Java程序将文件内容读出并显示到屏幕上

编写一个名为number1.txt的txt文档,内容包括100到109十个整数。编写一个Java程序将文件内容读出并显示到屏幕上,并以相反的顺序写入到文件number2.txt中。

package com.uf.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class Home {

public static void main(String[] args) {

    String inputFile = "C:\\Users\\wushucheng\\Desktop\\number1.txt";
    String outputFile = "C:\\Users\\wushucheng\\Desktop\\number2.txt";
    String content = getFileContent(inputFile);
    System.out.println(content);

    String[] array = content.split(" ");
    String[] newArray = new String[array.length];
    for (int i = 0; i < array.length; i++) {
        newArray[newArray.length - i - 1] = array[i];
    }
    String newContent = Arrays.toString(newArray).replaceAll("\\[|\\]", "");
    createFile(outputFile, newContent);
}

public static String getFileContent(String filePath){

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader(filePath));
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        try {
            while((temp = reader.readLine()) != null){
                stringBuffer.append(temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuffer.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally{
        if(reader != null){
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

public static void createFile(String filePath, String content){
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(new FileWriter(filePath));
        writer.write(content);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        if(writer != null){
            writer.close();
        }
    }
}

}

楼上代码亲测可以运行,number1.txt只的数字以空格分割并在一行。

/**
 * 文件的读取, 文件生成和写入内容Api
 * 
 * void
 */
public void test() {
    // 文件的读取
    File file = null;
    InputStream is = null;
    BufferedReader reader = null;

    FileWriterWithEncoding writerWithEncoding = null;
    BufferedWriter writer = null;
    try {
        file = new File("fileName.txt");
        is = new FileInputStream(file);
        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line = reader.readLine();

        // 文件生成和写入内容
        writerWithEncoding = new FileWriterWithEncoding(file, "UTF-8");
        writer = new BufferedWriter(writerWithEncoding);
        writer.write("");

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if(is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(writerWithEncoding != null) {
            try {
                writerWithEncoding.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}