We're learning file input and output in my programming class right now, but I have a Macbook Pro. I understand how to do it and I can do it on Windows but I'm having trouble finding out how to do it exactly for a Mac. I just can't seem to figure out what to put in the quotes for the 'File Fred = new File(" "); What do I need to put in the quotes to have it work? I have the file in the HDD named "David".
import java.util.Scanner;
import java.io.*;
public class FileIO
{
public static void main(String[] args)
{
File Fred = new File("David:\\mytext.txt");
try
{
Don't use "/" or "\" at all -- use System.getProperty("file.separator"). This will give you the correct character on the current OS. There are system properties that will help you write good cross-platform code. Check out System.getProperties() in the javadocs.
For example, to create "file.txt" in your home directory:
File myFile = new File(System.getProperty("user.home"), "file.txt");
That will work on OS X, Windows, and Linux.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With