导航:首页 > 操作系统 > java读取linux路径

java读取linux路径

发布时间:2022-09-04 11:25:06

‘壹’ java程序中怎么获取linxu系统的根目录

根目录的路径就是/,永远不变,直接写/即可,无需获取。

‘贰’ linux下 Java如何获取文件的绝对路径

需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}

‘叁’ linux下java读取文件路径怎么写

linux下文件路径都是“/”开始的,可以通过changeWorkingDirectory方法来进行路径的切换,举例:
**
* 上传文件
*
* @param fileName
* @param plainFilePath 文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
备注:只需要修改上传的服务器地址、用户名、密码即可进行服务器访问上传。根据实际需要修改即可。

‘肆’ java怎么取出linux服务器的文件路径

java可以获取当前项目的路径, Class.class.getClass().getResource("/").getPath()
这个是有请求的情况下,request.getSession().getServletContext().getRealPath("")

‘伍’ 用java如何读取linux中的某个文件

java是跨平台语言,在linux上读文件跟在windows上读文件是一样的 只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。
如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi /etc/profile,在文件末尾加上
export LANG="zh_CN.GBK"
export LC_ALL="zh_CN.GBK"
编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");

文件操作的核心代码请参考下面代码:

String path= "/home/";
path= "/home/multiverse/Repository/PMEPGImport";
File file=new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
//FileInputStream fis = new FileInputStream("fileName");

//InputStreamReader isr = new InputStreamReader(fis,"utf-8");
StringBuffer buffer = new StringBuffer();
String text;

BufferedReader input = new BufferedReader (new FileReader(tempList[i]));

while((text = input.readLine()) != null)
buffer.append(text +"/n"); }

if (tempList[i].isDirectory()) {
System.out.println("文件夹:"+tempList[i]);
}
}

‘陆’ java如何拼接linux目录下文件路径

java在读取linux目录时可以使用FileSystem类,FileSystem创建IO流时需要Path子类,新建Path只需要传入String类型的路径即可。
所以拼接路径实际上就是对String的拼接。
String有多种方法可以拼接,最简单的是直接用+号来接。

‘柒’ java获取linux路径怎么写

liunx 没有window中的盘符 只有一个根目录 不能用“\\” 会被转义 只能用“/”写 你用pwd命令查询 文件跟路径 然后拼文件全名 应该就可以的。。 试试

‘捌’ java如何获得linux下web路径

java获取根路径有两种方式:
1),在servlet可以用一下方法取得:
request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”) ”//upload//”;
2),不从jsp,或servlet中获取,只从普通java类中获取:
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
SAXReader() saxReader = new SAXReader();
if(path.indexOf(“WEB-INF”)>0){
path = path.substring(0,path.indexOf(“/WEB-INF/classes”) 16);
// ‘/WEB-INF/classes’为16位
document = saxReader.read(path filename);
}else{
document = saxReader.read(getClass().getResourceAsStream(filename));
}
weblogic tomcat 下都有效
String path =
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
<!--EndFragment-->

‘玖’ java linux怎么获取文件路径

一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中, 如何用绝对路径访问呢?
现在建立一个目录:
File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //这个就是绝对路径
File f=new File(fDir,strFile);

‘拾’ 用java如何读取linux中的某个文件

java是跨平台语言,在linux上读文件跟在windows上读文件是一样的 只是文件路径不一样,可以用File对象和FileInputSteam来读取。但要注意文件编码问题。
如果有中文请做适当的编码转换,通常情况下Linux的默认字符编码为UTF-8编码方式,项目可以直接采用utf8编码方式操作.用System.getProperty("file.encoding")可检查系统编码格式。可改操作系统的文件系统编码,vi /etc/profile,在文件末尾加上
export LANG="zh_CN.GBK"
export LC_ALL="zh_CN.GBK"
编码转换代码:new String(files[i].getName().getBytes("GBK"),"UTF-8");

文件操作的核心代码请参考下面代码:

String path= "/home/";
path= "/home/multiverse/Repository/PMEPGImport";
File file=new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
//FileInputStream fis = new FileInputStream("fileName");

//InputStreamReader isr = new InputStreamReader(fis,"utf-8");
StringBuffer buffer = new StringBuffer();
String text;

BufferedReader input = new BufferedReader (new FileReader(tempList[i]));

while((text = input.readLine()) != null)
buffer.append(text +"/n"); }

if (tempList[i].isDirectory()) {
System.out.println("文件夹:"+tempList[i]);
}
}

阅读全文

与java读取linux路径相关的资料

热点内容
秦九昭算法v0怎么求 浏览:380
斗鱼java 浏览:892
程序员对老师的感谢 浏览:27
什么app能查看银行卡照片 浏览:22
win7pdf虚拟打印 浏览:330
程序员喜欢的女生条件 浏览:123
阿里云服务器ip搭建教程 浏览:85
解压和拉伸这一动画的原理是什么 浏览:740
tbc战士的命令怒吼 浏览:481
idea快捷键看源码 浏览:976
手机碎屏解压工具 浏览:245
jsonrpcphp使用 浏览:566
网上求职系统源码 浏览:699
pdf数字不显示 浏览:890
convertwordtopdf 浏览:253
程序编译基本单位 浏览:23
python分析图片角度 浏览:64
阿里云服务器能复制数据吗 浏览:562
python拼音转换文字 浏览:563
动画遗传算法 浏览:63