java 如何读取目录下的文件内容

Java读取目录下的文件内容,使用的是java的文件类,示例如下:

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.RandomAccessFile;
importjava.io.Reader;

publicclassReadFromFile{
/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*
*@paramfileName
*文件的名
*/
(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
//一次读一个字节
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
//一次读多个字节
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//读入多个字节到字节数组中,byteread为一次读入的字节数
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*
*@paramfileName
*文件名
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
//一次读一个字符
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!=-1){
//对于windows下, 这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉 ,或者屏蔽 。否则,将会多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
//一次读多个字符
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while((charread=reader.read(tempchars))!=-1){
//同样屏蔽掉 不显示
if((charread==tempchars.length)
&&(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;i<charread;i++){
if(tempchars[i]==' '){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}

}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以行为单位读取文件,常用于读面向行的格式化文件
*
*@paramfileName
*文件名
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!=null){
//显示行号
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*随机读取文件内容
*
*@paramfileName
*文件名
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("随机读取一段文件内容:");
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"r");
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>4)?4:0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}

/**
*显示输入流中还剩的字节数
*
*@paramin
*/
(InputStreamin){
try{
System.out.println("当前字节输入流中的字节数为:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

⑵ 如何用java读取txt文件的内容

import java.io.*; public class Read{ public static void main(String [] args) { try{

FileReader f = new FileReader("D:\\1.txt"); // 注意路径这里可以有两种表示方法一个种\\一种/就可以了 BufferedReader buf = new BufferedReader(f);

String s; while((s = buf.readLine() ) != null){ System.out.println(s); } f.close(); buf.close();

}catch(IOException e){} }}

⑶ java如何获取文件信息

File 类是对文件和文件夹的抽象,包含了对文件和文件夹的多种属性和操作方法。File类的常用方法如下表:

返回
方法
说明

String getName 获取文件名称
String getParent 获取文件的父路径字符串
String getPath 获取文件的相对路径字符串
String getAbsolutePath 获取文件的绝对路径字符串
boolean exists 判断文件或者文件夹是否存在
boolean isFile 判断是不是文件类型
boolean isDirectory 判断是不是文件夹类型
boolean delete 删除文件或文件夹,如果删除成功返回结果为true
boolean mkdir 创建文件夹,创建成功返回true
boolean setReadOnly 设置文件或文件夹的只读属性
long length 获取文件的长度
long lastModified 获取文件的最后修改时间
String[ ] list 获取文件夹中的文件和子文件夹的名称,并存放到字符串数组中

⑷ java如何读取一个txt文件的所有内容

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

public static void main(String[] args) throws IOException {

String fileContent = readFileContent("");

System.out.println(fileContent);
}

//参数string为你的文件名
private static String readFileContent(String fileName) throws IOException {

File file = new File(fileName);

BufferedReader bf = new BufferedReader(new FileReader(file));

String content = "";
StringBuilder sb = new StringBuilder();

while(content != null){
content = bf.readLine();

if(content == null){
break;
}

sb.append(content.trim());
}

bf.close();
return sb.toString();
}
}
求点赞为满意回答。

⑸ java中怎样从一个文件中读取文件信息

java读取文件路径、所占空间大小等文件消息,主要是使用FileInputStream类来操作,示例如下:

importjava.io.File;
importjava.io.FileInputStream;

publicclassceshi{
publicstaticvoidmain(String[]args)throwsException{

java.io.FilelocalFile=newFile("D:\1.txt");
FileInputStreamins=newFileInputStream(localFile);
intcountLen=ins.available();
byte[]m_binArray=newbyte[countLen];
ins.read(m_binArray);
ins.close();
System.out.println(localFile.getAbsoluteFile()+""
+localFile.getFreeSpace());
}
}

运行结果如下:

⑹ Java如何读取txt文件的内容

publicclassMyword{
publicstaticvoidmain(String[]args)throwsIOException{
try{
FileInputStreamfile=newFileInputStream("e:/myText.txt");
BufferedInputStreamBfile=newBufferedInputStream(file);
byte[]b=newbyte[1024];
Strings="";
intbytesRead=0;
while((bytesRead=Bfile.read(b))!=-1){
s+=newString(b,0,bytesRead);
}
System.out.println(s);
}
}

⑺ java中在怎么读取文件夹中的内容

以下java程序的作用是将当前目录及其子目录中的.java文件收集到.txt文件中,并添加行号,你可以参考一下。
import java.io.*;
public class Collection
{
public static void main(String[] args) throws Exception
{
final String F=".\\collection.txt";

FW=new FileWriter(new File(F));
Collection.ProcessDirectory(new File("."));
Collection.FW.flush();
Collection.FW.close();
}

private static void ProcessDirectory(File d) throws Exception
{
File[] ds=null;

Collection.ProcessJavaFile(d);
ds=d.listFiles(Collection.DFilter);
for(int i=0;i<ds.length;i++)
{
Collection.ProcessDirectory(ds[i]);
}
}

private static void ProcessJavaFile(File d) throws Exception
{
String line=null;
LineNumberReader lnr=null;
File[] fs=d.listFiles(Collection.FNFilter);

for(int i=0;i<fs.length;i++)
{
lnr=new LineNumberReader(new FileReader(fs[i]));
Collection.FW.write(fs[i].getCanonicalPath()+"\r\n");
System.out.println(fs[i].getCanonicalPath());
while(null!=(line=lnr.readLine()))
{
Collection.FW.write(""+lnr.getLineNumber()+" "+line+"\r\n");
System.out.println(""+lnr.getLineNumber()+" "+line);
}
Collection.FW.write("\r\n");
System.out.println();
}
}

private static FileWriter FW;
private static FilenameFilter FNFilter=new FilenameFilter()
{
public boolean accept(File dir,String name)
{
return name.endsWith(".java");
}
};
private static FileFilter DFilter=new FileFilter()
{
public boolean accept(File pathname)
{
return pathname.isDirectory();
}
};
}

⑻ Java 如何读取txt文件的内容

能有的,很简单,readLine即可,然后封装到Map里面,key就是序号,value就是后面的值

⑼ JAVA怎样来获取上传的txt文件里面的内容

用两个页面来完成你的功能。
index.jsp接受你上传的文件;
uploadfile.jsp显示上传文件中的内容。
具体要显示什么,你根据自己需要修改下吧。
index.jsp的内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>index</title>
</head>
<body>
<center>
<form action="uploadfile.jsp" method = "post">
newFile: <input type = "file" name = "newFile" size=60 value=""/><br>
<input type = "submit" value = "upload">
</form>
</center>
</body>
</html>

----------------------------------
uploadfile.jsp内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import = "java.io.*" %>
<form action = "uploadfile.jsp" method = "post">
<table border = "1">
<tr>
<th>ID</th>
<th>UserName</th>
<th>Password</th>
</tr>

<%
try{
String s = request.getParameter("newFile");
String ss = new String(s.getBytes("ISO-8859-1"),"UTF-8");
File f = new File(ss);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String str = "";
int i = 1;
while((str = br.readLine()) != null){
ArrayList list = new ArrayList();
StringTokenizer st = new StringTokenizer(str, " ");
while(st.hasMoreElements()){
list.add((String)st.nextElement());
}
String u = (String)list.get(0);
String p = (String)list.get(1) ;
%>
<tr>
<td><%=i %></td>
<td><input type = "text" name = "u" value="<%=u %>"/></td>
<td><input type = "text" name = "p" value="<%=p %>"/></td>
</tr>
<%
i++;
}
br.close();
}catch(Exception e){
out.print(e);
}
%>
</table>
</form>

其实这种方式不是最理想的上传形式,不过要实现通用的那种方式的话,还需要下载一个jspSmartUpload组件来实现,这种方式是非常简洁的。

如果你要上传的txt文件的内容为:
aaa 111
bbbb 222
cccc 333
dddd 444
eeee 555
ffff 666

那么当你运行这个程序后,你在uploadfile.jsp页面上看到的输出结果是:
ID UserName Password
1 aaa 111
2 bbbb 222
3 cccc 333
4 dddd 444
5 eeee 555
6 ffff 666

现在这样的结果是你要的吗?

已经修改了,应该达到了你的目的了吧,不过了结果是在一个jsp页面中输出的。
姓名和密码分别放在两个不同的文本框中,如果txt有多个姓名和密码,那么就由多个文本框来分别存放姓名和密码。