Java读写文件和Scala读写文件

2021-04-27 12:01:46 浏览数 (1)

Java实现

代码语言:javascript复制
package com.xtd.test.java;

import java.io.*;

public class AppentFile {

    private static FileWriter fileWriter = null;
    private static FileReader fileReader = null;
    private static BufferedWriter bufferedWriter = null;
    private static BufferedReader bufferedReader = null;

    public static void main(String[] args) {
        // 写入文件的路径
        String filePath = "F:/IDEA-WorkSpace/spark";
        // 写入的文件名称
        String fileName = "MMSIFile";
        readAndWrite(filePath,fileName);
    }
    public static void readAndWrite(String filePath,String fileName){

        try {
            /************** 写文件 ************/
            // true:追加写入,flase:覆盖写入(默认覆盖写入)
            fileWriter = new FileWriter(filePath "/" fileName,true);
            // 对于小量数据少数写入使用 fileWriter 效率更高
            fileWriter.append("Java FileWriter Apend Write File!n");
            // 对于大量数据对此写入使用 bufferedWriter 效率更高
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("Java BufferedWriter Apend Write File!n");
            bufferedWriter.close();
            fileWriter.close();

            /************** 读文件 ************/
            fileReader = new FileReader(filePath "/" fileName);
            bufferedReader = new BufferedReader(fileReader);
            String str;
            while ( null != (str = bufferedReader.readLine()) ) {
                System.out.println(str);
            }
            bufferedReader.close();
            fileReader.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Scala实现

代码语言:javascript复制
package com.xtd.test.scala

import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter, IOException}

object AppentFile {

  private var fileWriter:FileWriter = null
  private var fileReader:FileReader = null
  private var bufferedWriter:BufferedWriter = null
  private var bufferedReader:BufferedReader = null

  def main(args: Array[String]): Unit = {
    // 写入文件的路径
    val filePath = "F:/IDEA-WorkSpace/spark"
    // 写入的文件名称
    val fileName = "MMSIFile"
    readAndWrite(filePath, fileName)
  }

  def readAndWrite(filePath: String, fileName: String): Unit = {
    try {
      /** ************ 写文件 ************/
      // true:追加写入,flase:覆盖写入(默认覆盖写入)
      fileWriter = new FileWriter(filePath   "/"   fileName, true)
      // 对于小量数据少数写入使用 fileWriter 效率更高
      fileWriter.append("Scala FileWriter Apend Write File!n")
      // 对于大量数据对此写入使用 bufferedWriter 效率更高
      bufferedWriter = new BufferedWriter(fileWriter)
      bufferedWriter.write("Scala BufferedWriter Apend Write File!n")
      bufferedWriter.close()
      fileWriter.close()

      /** ************ 读文件 ************/
      fileReader = new FileReader(filePath   "/"   fileName)
      bufferedReader = new BufferedReader(fileReader)
      var str:String = null
      while ( None!=(str = bufferedReader.readLine) && null!=str ) println(str)
      bufferedReader.close()
      fileReader.close()
    } catch {
      case e: IOException =>
        e.printStackTrace()
    }
  }
}

运行效果

Java FileWriter Apend Write File! Java BufferedWriter Apend Write File! Scala FileWriter Apend Write File! Scala BufferedWriter Apend Write File!

0 人点赞