我需要使用java programm打开文件夹中的文件 [英] I need to open files in a folder using java programm

查看:62
本文介绍了我需要使用java programm打开文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用java programm打开文件夹中的文件。



Ex:/ home / msr / hike / 25-08-2018 .....

文件夹中有多个文件我需要打开一个文件并显示内容



我尝试了什么:



包com.techstar.com;







import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Scanner;

import java.util.logging.Level;



import org.bson.diagnostics.Logger;





公共类FileOpen {

public static void main(String args [])抛出FileNotFoundException {

文件目录=新文件(/ home / msr / hike / 02-05-2018);

File [] listOfFiles = dir.listFiles();



for (int i = 0; i< listOfFiles.length; i ++ ){

if(listOfFiles [i] .isFile()){

System.out.println(File+ listOfFiles [i] .getName());

} else if(listOfFiles [i] .isDirectory()){

System.out.println(Directory+ listOfFiles [i] .getName());

}

}

}

}



我只获取文件名请帮我显示文件内容

输出:

文件02-05-2018_14-09-59_vikas_hike_Device_Info_027_output.txt

文件02-05-2018_21-30-50_kartheek_hike_hc_4cf_output.txt

文件02-05-2018_13-07-48_rakesh_hike_load_c41_output.txt

文件02-05-2018_21- 30-24_kartheek_hike_hi_e30_output.txt

文件02-05-2018_13-07-03_rakesh_hike_cpu_f57_output.txt

文件02-05-2018_21-30-46_kartheek_hike_ls_c82_output.txt

i need to open files in a folder using java programm.

Ex:/home/msr/hike/25-08-2018.....
there are multiple files in a folder i need to open a files and show content

What I have tried:

package com.techstar.com;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
import java.util.logging.Level;

import org.bson.diagnostics.Logger;


public class FileOpen {
public static void main (String args[]) throws FileNotFoundException {
File dir = new File("/home/msr/hike/02-05-2018");
File[] listOfFiles = dir.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
}
}

am getting only file names please help me need to display file content
output:
File 02-05-2018_14-09-59_vikas_hike_Device_Info_027_output.txt
File 02-05-2018_21-30-50_kartheek_hike_hc_4cf_output.txt
File 02-05-2018_13-07-48_rakesh_hike_load_c41_output.txt
File 02-05-2018_21-30-24_kartheek_hike_hi_e30_output.txt
File 02-05-2018_13-07-03_rakesh_hike_cpu_f57_output.txt
File 02-05-2018_21-30-46_kartheek_hike_ls_c82_output.txt

推荐答案

完全相同,因为所有文件都在文件夹中(即使在根目录下)。您已经知道如何获取列表,以便列表中的每个名称都可以打开并阅读内容。
Exactly the same, since all files are in folders (even at the root). You already know how to get the list so for each name in the list you can open and read the content.


这篇关于我需要使用java programm打开文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆