In the Windows development environment, you need to pay attention to the problem of using the Linux format path in Java code.

Source: Internet
Author: User

Note: the disk of the executed code file is the root directory.

Assuming that the compiled class file is on the e drive, the E: \ opt \ test.txt file under e can be written as/opt/test.txt in the code.

The advantage is that the code written in windows can be directly deployed on the linux server without changing the path.

Test code:

 

Package com. yanek. util; import java. io. bufferedReader; import java. io. file; import java. io. fileReader; import java. io. IOException; public class Test {/*** @ param args */public static void main (String [] args) {String path = "/opt/test.txt "; string c = readText (path); System. out. println ("c =" + c);}/*** read content from the file ** @ param filename * @ return */public static String readText (String filename) {String content = ""; tr Y {File file = new File (filename); if (file. exists () {FileReader fr = new FileReader (file); BufferedReader br = new BufferedReader (fr); String str = ""; String newline = ""; while (str = br. readLine ())! = Null) {content + = newline + str; newline = "\ n" ;}br. close (); fr. close () ;}} catch (IOException e) {e. printStackTrace () ;}return content ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.