简体   繁体   English

FileInputStream从特定位置读取文件?

[英]FileInputStream read file from specific location?

If I place text file in same project folder, program can read it without problems. 如果我将文本文件放在同一项目文件夹中,程序可以毫无问题地读取它。 But how can I make it read a file from somewhere in my computer (ex: in Desktop) 但是如何使它从计算机中的某个位置读取文件(例如,在桌面中)

FileInputStream fstream = new FileInputStream("Contact.txt");

I change it to "C:...\\Desktop\\Contact.txt") but I getting error. 我将其更改为“ C:... \\ Desktop \\ Contact.txt”),但出现错误。

Change the backslash ( \\ ) to forwardslash( / ) or use double backslashes ( \\\\ ) in your file path. 将反斜杠( \\ )更改为正斜杠( / )或在文件路径中使用双反斜杠( \\\\ )。

  FileInputStream fstream = new FileInputStream("C:.../Desktop/Contact.txt");

or 要么

  FileInputStream fstream = new FileInputStream("C:...\\Desktop\\Contact.txt");

Please Note: \\ is an escape character. 请注意: \\是转义字符。 If you use \\\\ , it will use single \\ as literal. 如果使用\\\\ ,它将使用单个\\作为文字。

You should use File.separator instead of / and \\\\ to delimit your path as this is will work on both Window and Unix based systems. 您应该使用File.separator而不是/\\\\来分隔路径,因为这在基于Window和Unix的系统上都可以使用。 This might help to convince you...! 可能有助于说服您...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM