Core Java · Chapter 37 of 48

Files and paths

By the end of this chapter you can read, write, append to and list files with Path and Files, and keep a file from being left open. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Beyond the basics
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Every counter writes its day's sales to a CSV file, one row per sale under the header station,item,qty,price. At closing time Ravi copies the files to a pen drive and adds them up in a spreadsheet, which takes him an hour a night. He wants the counter to read its own files and write the report.

So far every value your programs used was typed into the code or the terminal, and it was gone when the program ended. A List of sales lives only as long as the JVM. To keep a day's sales, and to read yesterday's, the program has to talk to the disk.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    SalesFiles.java: read a sales file into a list of sales

    In SalesFiles.java, make readSales(fileName) read the file with that name inside the counter's folder and return its sales as a list of Sale records, in file order. Skip the first line (the header station,item,qty,price) and every blank line, and turn each other line into a Sale with parse, which is already written. A file with only the header gives an empty list. A file that does not exist throws NoSuchFileException: let it leave the method.

    For example, a header followed by SBC,Masala chai,3,40, NDLS,Cutting chai,4,25, SBC,Bun maska,1,50, a blank line and CSMT,Masala chai,2,40 gives those four sales in that order.

  2. Task 2

    SalesFiles.java: total one station's sales, a line at a time

    In SalesFiles.java, make totalFor(fileName, station) read the file with that name inside the folder as a stream of lines, closed when the method is done, and return the total in rupees (the sum of amount()) of every sale whose station is exactly this station. Skip the header and blank lines. A station with no sales gives 0. A file that does not exist throws NoSuchFileException.

    For example, with the file from task 1, SBC gives 170 (3 x 40 + 1 x 50), NDLS gives 100 and MAS gives 0.

  3. Task 3

    SalesFiles.java: write a station's report into a reports folder

    In SalesFiles.java, make writeReport(station, sales) write the file report-<station>.txt into a folder named reports inside the counter's folder, making the reports folder when it is missing, and return the report file's path. The report is the line Report for <station>, then one line per sale of that station only, in list order, written as <item> x <qty> = ₹<amount>, then Total: ₹<sum>. Every line ends with a line break, the last one too. Writing a report again replaces the old file.

    For example, Masala chai 3 at 40 (SBC), Cutting chai 4 at 25 (NDLS) and Bun maska 1 at 50 (SBC) give, for SBC, the file <folder>/reports/report-SBC.txt holding Report for SBC, Masala chai x 3 = ₹120, Bun maska x 1 = ₹50 and Total: ₹170. A second report for SBC with only Cutting chai 2 at 25 leaves just Report for SBC, Cutting chai x 2 = ₹50 and Total: ₹50.

  4. Task 4

    SalesFiles.java: list the CSV files in the counter's folder

    In SalesFiles.java, make csvFiles() return the names (the file name only, not the whole path) of the regular files whose names end in .csv and that sit directly in the counter's folder, sorted A to Z. Leave out other files, folders (even a folder named old.csv) and anything inside sub-folders. An empty folder gives an empty list.

    For example, a folder holding sales-2026-10-04.csv, sales-2026-10-03.csv, notes.txt, a folder old.csv and a folder archive with sales-2026-09-30.csv in it gives [sales-2026-10-03.csv, sales-2026-10-04.csv].

  5. Task 5

    SalesFiles.java: add a sale to the end of a sales file

    In SalesFiles.java, make append(fileName, sale) add the sale's CSV row (from toCsv()) and a line break at the end of the file with that name inside the folder. When the file does not exist yet, create it with the header line station,item,qty,price and a line break first. A file that exists already keeps what it has and never gets a second header.

    For example, appending SBC Masala chai 3 at 40 and then SBC Bun maska 1 at 50 to a new file leaves station,item,qty,price, SBC,Masala chai,3,40 and SBC,Bun maska,1,50, each on its own line. Appending CSMT Cutting chai 1 at 25 to a file that already has a header and one row adds just CSMT,Cutting chai,1,25.

What you will learn

  • Path: where a file is, without touching the disk.
  • Reading: a whole file at once, or a stream of lines for big files.
  • Writing and appending: replacing a file, adding to its end, making folders.
  • Listing a folder: the entries directly inside it.

More in Beyond the basics

Build "Files and paths" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.