您的位置:首页 > 运维架构 > Linux

在windows开发环境中,java代码中使用linux格式路径的方法需要注意的问题

2013-02-07 17:12 1251 查看
注意点: 执行的代码文件所在盘为根目录即可。

假设 编译后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;
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐