2

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
    {   
1
  • 8
    crosses fingers that you don't get an ImSorryDaveICantLetYouDoThatException Commented Mar 8, 2011 at 20:07

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

/ is universally supported across all major platforms nowadays
3

On Mac OS, you use "/" (forward slash) and not "\" (backward slash) to separate files in a path. Hard drives are located under "/Volumes". So your file is probably /Volumes/David/mytext.txt .

2 Comments

Thank you. That worked, but now it's giving me the output I want plus a bunch of other stuff. {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350 {\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} \margl1440\margr1440\vieww9000\viewh8400\viewkind0 \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural \f0\fs24 \cf0 CIS 260 is cool.\ Let's put another sentence in here.\ Programming is problem driven.}
Your file is in RTF (Rich Text Format). Edit it with TextEdit and use TextEdit preferences to default to plain text.
1

new File("/"); is your root in the MacOS filesystem.

Just open a Terminal, cd to / and things will be revealed to you.

Comments

1

You won't use the same type of file path string you would on Windows. AFAIK your Mac should treat file paths the same as other *nix operating systems, with the "top" of the file system being / rather than C:\.

You can also address files as relative paths, rather than absolute paths. If you run your Java program from some directory ./program, with your text file in ./program/file.txt, then your argument is simply new File("file.txt");

Comments

0

Try just dragging your file from your desktop, to a terminal page. This will give you the exact path you need to type into you Java program. Also use text edit's format Make Plain Text to make sure it is in the correct form to be read by Java.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.