Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
16
Help us understand the problem. What is going on with this article?
@nimani76

kotlinでテキストファイルの読み込み、書き出し

More than 1 year has passed since last update.

前提

test.txt
1 2 3 4 5

このファイルへの読み込み書き出し

読み込み

Scannerでの読み込み

try{
    val file = File("パス")
    val scan = Scanner(file)
    var i = scan.nextLine()
    i = scan.next()
}catch (e: FileNotFoundException){
    println(e)
}

FileReaderでの読み込み

try{
    val file = File("パス")
    val filereader = FileReader(file)
    var rd: Int = filereader.read()
    while (rd != -1) {
        print(rd.toChar())
        rd = filereader.read()
    }
}catch (e: FileNotFoundException){
    println(e)
}

書き出し

    val fil = FileWriter("パス")
    val pw = PrintWriter(BufferedWriter(fil))
    pw.println("3")
    pw.println("5")
    pw.println("7")
    pw.close()

出力後

test.txt
3
5
7

自分用のメモに

16
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
nimani76

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
16
Help us understand the problem. What is going on with this article?