閱讀517 返回首頁    go 阿裏雲 go 技術社區[雲棲]


在windows開發環境中,java代碼中使用linux格式路徑的方法需要注意的問題

注意點: 執行的代碼文件所在盤為根目錄即可。


假設 編譯後class文件在e盤,則e下的 E:\opt\test.txt  在代碼中就可以寫成/opt/test.txt

這樣的好處是 windows下寫的代碼直接部署到linux服務器就可以了,路徑不用改。

測試代碼:

 

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);
		

	}
	
	/**
	 * 從文件讀取內容
	 * 
	 * @param filename
	 * @return
	 */
	public static String readText(String filename) {
		String content = "";
		try {
			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;
	}

}


 

最後更新:2017-04-04 07:03:45

  上一篇:go poj 1258 Agri-Net MST
  下一篇:go java之JUC係列-外部Tools-Executors|Semaphor|Exchanger|CyclicBarrier|CountDownLatch