macos Java for Mac 中的文件输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5237663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:48:12  来源:igfitidea点击:

File input in Java for Mac

javamacosfile-io

提问by Mary

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".

我们现在正在我的编程课上学习文件输入和输出,但我有一台 Macbook Pro。我知道如何做到这一点,我可以在 Windows 上做到这一点,但我很难找到如何在 Mac 上做到这一点。我似乎无法弄清楚在 'File Fred = new File(" "); 的引号中放什么。我需要在引号中加上什么才能让它工作?我在硬盘驱动器中有名为“大卫”的文件。

import java.util.Scanner;
import java.io.*;

public class FileIO
{

public static void main(String[] args)
{

    File Fred = new File("David:\mytext.txt");
    try
    {   

回答by Jeremy Brooks

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.

根本不要使用“/”或“\”——使用 System.getProperty("file.separator")。这将为您提供当前操作系统上的正确字符。有一些系统属性可以帮助您编写好的跨平台代码。查看 javadoc 中的 System.getProperties()。

For example, to create "file.txt" in your home directory:

例如,要在您的主目录中创建“file.txt”:

File myFile = new File(System.getProperty("user.home"), "file.txt");

File myFile = new File(System.getProperty("user.home"), "file.txt");

That will work on OS X, Windows, and Linux.

这将适用于 OS X、Windows 和 Linux。

回答by Spike Gronim

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 .

在 Mac OS 上,您使用“/”(正斜杠)而不是“\”(反斜杠)来分隔路径中的文件。硬盘驱动器位于“/Volumes”下。所以你的文件可能是 /Volumes/David/mytext.txt 。

回答by Jochen Bedersdorfer

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

new File("/");是您在 MacOS 文件系统中的根。

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

只需打开一个终端,cd 到 /,事情就会向你显示。

回答by Tom G

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:\.

您不会使用与 Windows 上相同类型的文件路径字符串。AFAIK 你的 Mac 应该像对待其他 *nix 操作系统一样对待文件路径,文件系统的“顶部”是/而不是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");

您还可以将文件寻址为相对路径,而不是绝对路径。如果您从某个目录运行 Java 程序./program,并在其中包含文本文件./program/file.txt,那么您的参数就是new File("file.txt");

回答by Taylor Attix

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 formatMake Plain Textto make sure it is in the correct form to be read by Java.

尝试将文件从桌面拖到终端页面。这将为您提供在 Java 程序中键入所需的确切路径。还可以使用文本编辑formatMake Plain Text来确保它是由 Java 读取的正确格式。