Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 4

Files

In Java, there are several opportunities for communicate with files. In this section, we will get to know only one way how to get data from a file and write data into a file.

import java.io.File;            // import package that handles Files
import java.io.PrintWriter;     // import package that can write into a file
import java.util.Scanner;       // import package that can read from a file


public class ReadWriteFile {

    // add two words into the header of main method to handle errors (will study in detail later)
    public static void main(String[] args) throws Exception {

        // create an instance of File class and define the file to create connection with
        File myFile = new File("c:/temp/mydata.txt");

        // check if the file already exists
        if (myFile.exists())
            System.out.println("This file already exists in this folder.");
        else
            System.out.println("This file does not exist in this folder.");

        // to write into a file, create an instance of PrintWriter class 
        // the arguments are: the reference to the file and encording
        PrintWriter pw = new PrintWriter(myFile, "UTF-8");

        // write into the file
        pw.print("Kerese ");
        pw.println("street");

        // close the connection witht he file
        pw.close();


        // to read from a file, create an instance of Scanner class and
        // the arguments are: the reference to the file and encording
        Scanner sc = new Scanner(myFile, "UTF-8");

        // read data from the file using a loop
        while (sc.hasNextLine()) {  // method hasNextLine returns a boolean value
                                    // if the Scanner has another line in its input to read
            String line = sc.nextLine(); // method nextLine returns the line that was read
            System.out.println(line);
        }

        // close the connection with the file
        sc.close();

    }
}

Useful link: We can apply various methods to instance myFile. Study the methods applicable to the instances of File class here.

Useful link: The full list of PrintWriter class fields, constructors, methods and their description can be found here.

Useful link: Read about different encodings here.

Session 4
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment