在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;
	}

}


 

目录
相关文章
|
5月前
|
安全 Linux 生物认证
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
234 0
Nexpose 8.25.0 for Linux & Windows - 漏洞扫描
|
5月前
|
安全 Linux 生物认证
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
292 17
Nexpose 8.24.0 for Linux & Windows - 漏洞扫描
|
5月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
287 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
5月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
503 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
5月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
1104 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
Linux 开发工具 数据安全/隐私保护
嵌入式Linux开发环境搭建之十---阿里云服务器购买及Linux开发环境搭建
嵌入式Linux开发环境搭建之十---阿里云服务器购买及Linux开发环境搭建
341 0
|
Ubuntu Linux 开发工具
嵌入式Linux开发环境搭建之四----Vim的安装
嵌入式Linux开发环境搭建之四----Vim的安装
254 0
|
Ubuntu Linux 虚拟化
嵌入式Linux开发环境搭建之一---VMware Ubuntu安装教程(亲自实践)
嵌入式Linux开发环境搭建之一---VMware Ubuntu安装教程(亲自实践)
599 0
|
Ubuntu Linux 数据安全/隐私保护
嵌入式Linux开发环境搭建之六---搭建Samba服务器
嵌入式Linux开发环境搭建之六---搭建Samba服务器
419 1
|
Ubuntu Linux 虚拟化
嵌入式Linux系列第1篇:开发环境搭建
Ublox ZED-F9P 厘米级定位模组开箱评估