Easily Open a File in Java

Say you wanted to open a file in Java, either for reading or writing. But it’s not clear how to do it, and whether you need a GUI, and blah blah blah, wouldn’t it be nice to just find a minimal example?  Well then, here you go.

import javax.swing.*;   // for JFileChooser
import java.io.*;       // for File
import java.util.*;     // for Scanner (if you want to read it in)

class FileOpener {

    public static void main(String[] args) {
        JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();
            // it's in your hands now...

Solid. Now say you want to read from the file:

            try {
                // read in the file
                Scanner s = new Scanner(f);
                String str = s.nextLine();
                // ...
            } catch(FileNotFoundException e) {
                // Couldn't find the file for some reason

Or writing to the file:

            try {
                FileWriter fw = new FileWriter(f);
                fw.write("Hello, World!");
                // ...
            } catch(IOException e) {
                // didn't work

Ta-da! Protip: If you’re using this as part of a GUI with a Component “comp” showing your main GUI screen, set “comp” to be the parent of the filechooser dialog by calling fc.showOpenDialog(comp);.

Other protip: if you really want, you can subject yourself to the official tutorial here.

Build Your Own Computer Part 4: Put It Together

Putting together a computer should be easy. In fact, it is easy. So let’s do it.

If you’re interested in the entire process of building your own computer, starting from scratch, then I encourage you to check out Part 1, Part 2, and Part 3. They detail the steps of understanding and choosing your components. But if you’ve got all the components and are ready to install, then it’s time for…

Continue reading

Build Your Own Computer Part 3: Choosing the Parts

So you’re interested in building your own computer. And you’ve already read through Part 1 (The Basics) and Part 2 (Understanding the Options).

Those posts covered these topics: Timestamp (this article is up to date as of July 2011), Am I Qualified (Yes), Reasons to Build It Yourself, List the Parts, and Understand the Parts.

So we know, generally speaking, what’s going into our computer. More than that, we know what our options are at each step and about what they’ll cost us. Now’s the time to start making decisions. Continue reading

Build Your Own Computer Part 2: Understanding the Options

You want to build your own computer, right? Of course you do. By now, I’m certain that you’ve read Part 1: The Basics. If you know what the parts of a computer are, you probably skipped it. The main point I want to recapitulate is that this article was written in March 2011. [UPDATED: current as of July 2011.]  In this business, you’ve got to use recent sources.

Previously, we looked at what components go into building a PC. Now we’re going to go over that list another time, but in detail. Our goal here is to understand the options available to us when it comes time to pick out parts and make a budget.

Continue reading

Build Your Own Computer Part 1 – The Basics

Building your own computer is a piece of cake, and can be really useful besides. But it can be tough to figure out where to start. Processors? Motherboards? RAM? This post is the first of a series that will guide you through the steps to take if you’re interested in building your own computer. We’ll go from understanding the different parts of the computer, to selecting and ordering parts, all the way to installation and setup.

In Part 1, we’ll look at exactly what parts are required and what they do.

Part 2 goes a little more in-depth on the technical aspects and options available.

Part 3 offers a guide on how to pick out your parts, once you know your choices.

Part 4 is a guide on how to actually put the thing together.

This is what we're going for

Continue reading