FileReader 和 FileWriter(Second)

本文介绍如何使用Java中的FileReader和FileWriter进行文件复制,并提供了完整的代码示例。作者为一名大二计算机专业学生,通过自学分享了这一实用教程。

一.简介

本人大二计算机专业生,Java在b站大学的动力节点(对初学者非常友好)自学的,毕竟大学嘛,懂的都懂,不能依靠老师,还得自己来,在本章节内如中分享我自学IO流这章的的学习笔记,毕竟学习路上从不缺少经验的分享者,希望大家一键三连支持一下!!!!

二.FileReader 和 FileWriter使用

FileReader 和 FileWriter 与FileInputStream、FileOutputStream用法相同,只不过把
byte数组换成char数组

以下代码为:利用FileReader和FileWriter (字符流)复制文件一个例子:


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
FileReader 和 FileWriter 与FileInputStream、FileOutputStream用法相同,只不过把
byte数组换成char数组
利用FileReader和FileWriter (字符流)复制文件:

 */
public class FileCopyTest02 {
    public static void main(String[] args) {
        FileReader reader=null;
        FileWriter writer=null;

        try {
            reader=new FileReader("src\\JavaSe_IO\\TestRearWriter");
            writer=new FileWriter("TestReaderWriter",true);
            int countReader=0;
            char[] chars=new char[10];
            while((countReader=reader.read(chars))!=-1){
                writer.write(chars,0,countReader);//0-countReader读多少写多少

            }
            writer.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

自我总结:

新的知识点学习完后需要通过亲手敲代码,去实现一下,毕竟实践是检验真理的唯一标准,之后我也会更新IO流的其他内容放在JavaSE IO专栏中希望大家一键三连支持一下!!!

import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.math.BigDecimal; import java.util.HashMap; import java.util.LinkedList; public class Test_GNSS { public static void main(String[] args) throws IOException { double miu = 3.986004418e+14; //CGCS2000 坐标系下的地球引力常数 double OmegaEdot = 7.2921150e-5; //CGCS2000 坐标系下的地球旋转速率 double DayToSecond = 24.0 * 3600.0d; //天秒数 //创建字符输入输对象来访问文件 FileReader fileReader = new FileReader("D:\\juechen\\badata.txt"); FileWriter fileWriter = new FileWriter("D:\\juechen\\badata_result.txt"); //创建输入对象的缓冲流 BufferedReader bufferedReader = new BufferedReader(fileReader); //将缓冲流写入字符串 String inputData = bufferedReader.readLine(); //System.out.println(inputData); //创建字符可变的字符串用来记录计算结果 StringBuilder outputDta = new StringBuilder("PRN\tx\ty\tz\n"); //创建相应的数据结构用来保存原始数据 LinkedList<HashMap<String, String>> linkedList = new LinkedList<>(); //将卫星星历数据存储在LinkedList中的计算过程 for (int i = 0; i < 13; i++) { inputData = bufferedReader.readLine(); //将读入的字符串保存在字符串数组中,当匹配到两个及以上的空格时,分割 String[] newLineArray = inputData.split("[ ]{2,}"); //将列名所对应的数据存储在HashMap中 HashMap<String, String> newLineHashMap = new HashMap<>(); newLineHashMap.put("Epoch", newLineArray[0]); newLineHashMap.put("PRN", newLineArray[1]); newLineHashMap.put("WN", newLineArray[2]); newLineHashMap.put("toe", newLineArray[3]); //对超过16位有效位的数用BigDecimal进行有效存储,提高精度 BigDecimal db = new BigDecimal(newLineArray[4]); newLineHashMap.put("SqrtA", db.toPlainString()); db = new BigDecimal(newLineArray[5]); newLineHashMap.put("e", db.toPlainString()); db = new BigDecimal(newLineArray[6]); newLineHashMap.put("i", db.toPlainString()); db = new BigDecimal(newLineArray[7]); newLineHashMap.put("Omega0", db.toPlainString()); db = new BigDecimal(newLineArray[8]); newLineHashMap.put("w", db.toPlainString()); db = new BigDecimal(newLineArray[9]); newLineHashMap.put("M", db.toPlainString()); db = new BigDecimal(newLineArray[10]); newLineHashMap.put("deltaN", db.toPlainString()); db = new BigDecimal(newLineArray[11]); newLineHashMap.put("OmegaDot", db.toPlainString()); db = new BigDecimal(newLineArray[12]); newLineHashMap.put("idot", db.toPlainString()); newLineHashMap.put("Crs", newLineArray[13]); newLineHashMap.put("Crc", newLineArray[14]); db = new BigDecimal(newLineArray[15]); newLineHashMap.put("Cus", db.toPlainString()); db = new BigDecimal(newLineArray[16]); newLineHashMap.put("Cuc", db.toPlainString()); db = new BigDecimal(newLineArray[17]); newLineHashMap.put("Cis", db.toPlainString()); db = new BigDecimal(newLineArray[18]); newLineHashMap.put("Cic", db.toPlainString()); newLineHashMap.put("toc", newLineArray[19]); db = new BigDecimal(newLineArray[20]); newLineHashMap.put("a0", db.toPlainString()); db = new BigDecimal(newLineArray[21]); newLineHashMap.put("a1", db.toPlainString()); db = new BigDecimal(newLineArray[22]); newLineHashMap.put("a3", db.toPlainString()); //保存原始数据 linkedList.add(newLineHashMap); } //用linkList存储的每一组卫星星历数据来计算卫星位置 for (int i = 0; i < 13; i++) { HashMap<String, String> calculateHashMap = linkedList.get(i); //计算观测时间的儒略日 String[] date_time = calculateHashMap.get("Epoch").split(" "); String date = date_time[0]; String time = date_time[1]; double[] timeD = new double[]{Double.parseDouble(date.split("-")[0]), Double.parseDouble(date.split("-")[1]), Double.parseDouble(date.split("-")[2])}; double[] timeH = new double[]{Double.parseDouble(time.split(":")[0]), Double.parseDouble(time.split(":")[1]), Double.parseDouble(time.split(":")[2])}; double EpochT = calculateJD(timeD[0], timeD[1], timeD[2], timeH[0], timeH[1], timeH[2]) - calculateJD(2006d, 1d, 1d, 0, 0, 0); //计算半长轴 double A = Math.pow(Double.parseDouble(calculateHashMap.get("SqrtA")), 2); //计算卫星平均角速度 double n0 = Math.sqrt(miu / Math.pow(A, 3)); //计算观测历元到参考历元的时间差 double tk = (EpochT - Double.parseDouble(calculateHashMap.get("WN")) * 7.0d) * DayToSecond - Double.parseDouble(calculateHashMap.get("toe")); //计算改正平均角速度 double n = n0 + Double.parseDouble(calculateHashMadouble year = y + 4800; double m = mon; if (m < 2) { m = m + 12; year = year - 1; } double a = Math.floor(30.6 * (m + 1)); double b = 0; if (y < 1582 || (y == 1582 && mon < 10) || (y == 1582 && mon == 10 && d < 5)) { b = -38; } else { b = Math.floor((y / 400) - (y / 100)); } double c = Math.floor(365.25 * year); return (a + b + c + day); } //矩阵乘法 static double[][] matrixMultiplication(double[][] A, double[][] B) { int line = A.length; //矩阵A第一维长度() int column = B[0].length; //矩阵B第二维长度() double[][] result = new double[line][column]; //定位结果矩阵大小 for (int i = 0; i < A.length; i++) { //矩阵A的行数 for (int j = 0; j < B[0].length; j++) { //矩阵B的列数 double sum = 0; //矩阵的每一行乘以每一列 for (int k = 0; k < B.length; k++) { sum += A[i][k] * B[k][j]; } result[i][j] = sum; } } return result; }改为Python语言
最新发布
06-11
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值