如何打开文本文件? [英] How to open a text file?

查看:51
本文介绍了如何打开文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一辈子都搞不清这个程序有什么问题:

 import java.io.*;公共类 EncyptionAssignment{public static void main (String[] args) 抛出 IOException{字符串线;BufferedReader 中;in = new BufferedReader(new FileReader("notepad encypt.me.txt"));line = in.readLine();而(行!= null){System.out.println(line);line = in.readLine();}System.out.println(line);}}

错误消息说找不到该文件,但我知道该文件已存在.我需要将文件保存在特殊文件夹中吗?

解决方案

错误是"notepad encypt.me.txt".由于您的文件名为encypt.me.txt",因此您不能在其名称前放置记事本".另外,可能是notepad encypt.me.txt这个文件不存在或者不是你想打开的.

此外,如果文件不在您的项目文件夹中,您必须提供文件的路径(绝对或相对).

我假设您使用的是 Microsoft Windows 系统.

如果您的文件的绝对路径为C:\foo\bar\encypt.me.txt",则必须将其作为 "C:\\foo\\bar\\encypt.me 传递.txt""C:"+File.separatorChar+"foo"+File.separatorChar+"bar"+File.separatorChar+encypt.me.txt".

如果它仍然不起作用,您应该验证该文件:

1) 存在于提供的路径中.您可以使用以下代码来实现:

File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt");System.out.println(encyptFile.exists());

如果提供的路径是正确的,则应该为 true.

2) 可以被应用程序读取

您可以使用以下代码来实现:

File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt");System.out.println(encyptFile.canRead());

如果你有读取文件的权限,应该是true.

更多信息:

文件的Javadoc

关于计算路径的信息

I can't figure out for the life of me what is wrong with this program:

     import java.io.*;

     public class EncyptionAssignment 
     {
         public static void main (String[] args) throws IOException
         {
             String line;
             BufferedReader in;

             in = new BufferedReader(new FileReader("notepad encypt.me.txt"));
             line = in.readLine();

             while(line != null)
             {
                    System.out.println(line);
                    line = in.readLine();
             }

             System.out.println(line);
         }

    }

The error message says that the file can't be found, but I know that the file already exists. Do I need to save the file in a special folder?

解决方案

The error is "notepad encypt.me.txt". Since your file is named "encypt.me.txt", you can't put a "notepad" in front of its name. Moreover, the file named "notepad encypt.me.txt" probably didn't exist or is not the one that you want to open.

Additionally, you have to provide the path ( absolute or relative ) of your file if it's not located in your project folder.

I will take the hypothesis that your are on a Microsoft Windows system.

If your file has as absolute path of "C:\foo\bar\encypt.me.txt", you will have to pass it as "C:\\foo\\bar\\encypt.me.txt" or as "C:"+File.separatorChar+"foo"+File.separatorChar+"bar"+File.separatorChar+encypt.me.txt".

If it's still not working, you should verify that the file :

1) Exist at the path provided. You can do it by using the following piece of code:

File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt");
System.out.println(encyptFile.exists());

If the path provided is the right one, it should be at true.

2) Can be read by the application

You can do it by using the following piece of code:

File encyptFile=new File("C:\\foo\\bar\\encypt.me.txt");
System.out.println(encyptFile.canRead());

If you have the permission to read the file, it should be at true.

More informations:

Javadoc of File

Informations about Path in computing

这篇关于如何打开文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆