当前位置:首页 » 编程语言 » java读取本地文件

java读取本地文件

发布时间: 2022-06-09 10:56:17

A. java根据路径读取文件

其读取方法为:

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.ArrayList;


publicclassreadFile{
privatestaticArrayList<String>listname=newArrayList<String>();
publicstaticvoidmain(String[]args)throwsException{
readAllFile("C:/Users/HP/Desktop");
System.out.println(listname.size());
}
publicstaticvoidreadAllFile(Stringfilepath){
Filefile=newFile(filepath);
if(!file.isDirectory()){
listname.add(file.getName());
}elseif(file.isDirectory()){
System.out.println("文件");
String[]filelist=file.list();
for(inti=0;i<filelist.length;i++){
Filereadfile=newFile(filepath);
if(!readfile.isDirectory()){
listname.add(readfile.getName());
}elseif(readfile.isDirectory()){
readAllFile(filepath+"\"+filelist[i]);//递归
}
}
}
for(inti=0;i<listname.size();i++){
System.out.println(listname.get(i));
}
}
}

B. java中文件的读取实现,以及用到哪些类

ava.io包中包括许多类提供许多有关文件的各个方面操作。
1 输入输出抽象基类InputStream/OutputStream ,实现文件内容操作的基本功能函数read()、 write()、close()、skip()等;一般都是创建出其派生类对象(完成指定的特殊功能)来实现文件读写。在文件读写的编程过程中主要应该注意异常处理的技术。
2 FileInputStream/FileOutputStream:
用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象);
本地文件读写编程的基本过程为:
① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);
② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;
③ 关闭文件(close())。
3 PipedInputStream/PipedOutputStream:
用于管道输入输出(将一个程序或一个线程的输出结果直接连接到另一个程序或一个线程的输入端口,实现两者数据直接传送。操作时需要连结);
4管道的连接:
方法之一是通过构造函数直接将某一个程序的输出作为另一个程序的输入,在定义对象时指明目标管道对象
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream(pInput);
方法之二是利用双方类中的任一个成员函数 connect()相连接
PipedInputStream pInput=new PipedInputStream();
PipedOutputStream pOutput= new PipedOutputStream();
pinput.connect(pOutput);
5 管道的输入与输出:
输出管道对象调用write()成员函数输出数据(即向管道的输入端发送数据);而输入管道对象调用read()成员函数可以读起数据(即从输出管道中获得数据)。这主要是借助系统所提供的缓冲机制来实现的。
6随机文件读写:
RandomAccessFile类(它直接继承于Object类而非InputStream/OutputStream类),从而可以实现读写文件中任何位置中的数据(只需要改变文件的读写位置的指针)。
随机文件读写编程的基本过程为:
① 生成流对象并且指明读写类型;
② 移动读写位置;
③ 读写文件内容;
④ 关闭文件。

C. java读写本地文件

java.io包
FileInputStream
FileOutputStream
XML,可以使用DOM、XPATH,,,,,JAVA内置也有这样的包。

D. java怎么获取本地文件路径

写了一个读取本地文件的方法, File file = new File(htmlFile); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while((s=br.readLine())!=null){ al.add(s); } 在当前类写了main方法测试了一下是可行的, 但是页面某方法想调用该方法,不能实现。 总结问题是:只有放在static方法中可行,在其他地方调用都显示找不到指定文件。 文件结构: 把本地文件放在了web-inf的classes下了,相对路径写的(“/file.txt”); 求教为啥static方法可以,其他地方调用不行,这个函数本身不是静态的啊。

E. java怎样读取本地文件夹下的文件

Filefile=newFile("WebRoot\test.html");
BufferedReaderbufferedReader=
newBufferedReader(newInputStreamReader(newFileInputStream(file)));
Stringrow=null;
StringBuffersb=newStringBuffer();
while((row=bufferedReader.readLine())!=null){
System.out.println(row);
sb.append(row);
}
bufferedReader.close();

F. java applet能否读取本地文件

applet是运行在客户端的虚拟机的,出于安全角度来说,applet本部可以读取客户端文件的,但是applet可以通过数字签名,好像还需要配置一些的安全策略还是可以实现的,网上应该有不少这种例子

G. java能读写本地hosts文件么

java能读写本地hosts文件, java web开发之本地hosts文件配置 在进行web开发的时候,通常需要以http://localhost或者127.0.0.1之类的访问本地环境的网站。

H. 用java 读取本地磁盘下的一个txt文件

importjava.io.BufferedInputStream;
importjava.io.FileInputStream;
importjava.io.IOException;

{
publicstaticvoidmain(String[]args)throwsIOException{
//BufferedInputStream(InputStreamin)
BufferedInputStreambis=newBufferedInputStream(newFileInputStream(
"bos.txt"));

//读取数据
//intby=0;
//while((by=bis.read())!=-1){
//System.out.print((char)by);
//}
//System.out.println("---------");

byte[]bys=newbyte[1024];
intlen=0;
while((len=bis.read(bys))!=-1){
System.out.print(newString(bys,0,len));
}

//释放资源
bis.close();
}
}

I. java的几种IO流读取文件方式

一、超类:


字节流: InputStream(读入流) OutputStream(写出流)


字符流: Reader(字符 读入流) Writer (字符写出流)

二、文件操作流


字节流: FileInputStream ,FileOutputStream


字符流: FileReader, FileWriter(用法与字节流基本相同,不写)

//1.指定要读 的文件目录及名称


File file =new File("文件路径");


//2.创建文件读入流对象


FileInputStream fis =new FileInputStream(file);


//3.定义结束标志,可用字节数组读取


int i =0 ;


while((i = fis.read())!=-1){


//i 就是从文件中读取的字节,读完后返回-1


}


//4.关闭流


fis.close();


//5.处理异常

//1.指定要写到的文件目录及名称


File file =new File("文件路径");


//2.创建文件读入流对象


FileOutputStream fos =new FileOutputStream(file);


//3.定义结束标志


fos.write(要写出的字节或者字节数组);


//4.刷新和关闭流


fos.flush();


fos.close();


//5.处理异常

三、缓冲流:


字节缓冲流: BufferedInputStream,BufferedOutputStream


字符缓冲流:BufferedReader ,BufferedWriter


缓冲流是对流的操作的功能的加强,提高了数据的读写效率。既然缓冲流是对流的功能和读写效率的加强和提高,所以在创建缓冲流的对象时应该要传入要加强的流对象。

//1.指定要读 的文件目录及名称


File file =new File("文件路径");


//2.创建文件读入流对象


FileInputStream fis =new FileInputStream(file);


//3.创建缓冲流对象加强fis功能


BufferedInputStream bis =new BufferedInputStream(fis);


//4.定义结束标志,可用字节数组读取


int i =0 ;


while((i = bis.read())!=-1){


//i 就是从文件中读取的字节,读完后返回-1


}


//5.关闭流


bis.close();


//6.处理异常

//1.指定要写到的文件目录及名称


File file =new File("文件路径");


//2.创建文件读入流对象


FileOutputStream fos =new FileOutputStream(file);


//3.创建缓冲流对象加强fos功能


BufferedOutputStream bos=new BufferedOutputStream(fos);


//4.向流中写入数据


bos.write(要写出的字节或者字节数组);


//5.刷新和关闭流


bos.flush();


bos.close();


//6.处理异常

四、对象流


ObjectInputStream ,ObjectOutputStream


不同于以上两种类型的流这里只能用字节对对象进行操作原因可以看上篇的编码表比照原理

ObjectOutputStream对象的序列化:


将java程序中的对象写到本地磁盘里用ObjectOutputStream


eg:将Person类的对象序列化到磁盘

  1. 创建Person类


    注1:此类要实现Serializable接口,此接口为标志性接口


    注2:此类要有无参的构造函数


    注3:一旦序列化此类不能再修改


    class Person implements Serializable{


    public Person(){}


    }


    2.创建对象流对象


    注:要增强功能可以将传入文件缓冲流


    ObjectOutputStream oos =new ObjectOutputStream(


    new FileOutputStream(new File("文件路径")));


    3.写入对象 ,一般会将对象用集合存储起来然后直接将集合写入文件


    List<Person> list =new ArrayList<>();


    list.add(new Person());


    ...(可以添加多个)


    oos.writeObject(list);


    4.关闭流,处理异常


    oos.flush();


    oos.close();

五、转换流:

这类流是用于将字符转换为字节输入输出,用于操作字符文件,属于字符流的子类,所以后缀为reader,writer;前缀inputstream,outputstream;

注 :要传入字节流作为参赛


InputStreamReader: 字符转换输出流


OutputStreamWriter:字符转换输入流

//1.获取键盘输入的字节流对象

inInputStream in =Stream.in;

/*2.用转换流将字节流对象转换为字符流对象,方便调用字符缓冲流的readeLine()方法*/


InputStreamReader isr =new InputStreamReader(in);


/*5.创建字符转换输出流对象osw,方便把输入的字符流转换为字节输出到本地文件。*/


OutputStreamWriter osw =new OutputStreamWriter(new
FileOutputStream(new File("文件名")));



/*3.现在isr是字符流,可以作为参数传入字符缓冲流中*/


BufferedReader br =new BufferedReader(isr);

/*4.可以调用字符缓冲流br的readLine()方法度一行输入文本*/


String line =null;


while((line =br.readLine()){


osw.write(line);//osw是字符流对象,可以直接操作字符串

}



注:InputStreamReader isr =new InputStreamReader(new "各种类型的字节输入流都行即是:后缀为InputStream就行");


OutputStreamWriter osw =new OutputStreamWriter(new
"后缀为OutputStream就行");

六、区别记忆


1.对象流是可以读写几乎所有类型的只要是对象就行,而字节字符流,只能读写单个字节字符或者字节字符数组,以上没有读写字节字符数组的;注意对象流只有字节流!


2.字符和字节循环读入的结束条件int i=0; (i =fis.read())!=-1
用字符数组复制文件(fr 读入流 ,fw写出流),字节流也是相同的用法

int i = 0; char[] c = new char[1024];


while((i = fr.reade()) !=-1)){


fw.write(c,0,i);


}

123456

3.对象流里面套缓冲流的情景:


new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(“文件路径”))));

4.记忆流及其功能的方法:


前缀表示功能,后缀表示流的类型;


比如说FileInputStream 前缀:File,表示操作的磁盘,后缀:intputstream,表示是字节输入流。


同理 FileReader:表示操作文件的字符流


ObjectInputStream :操作对象的字节输入流

5.拓展:获取键盘输入的字符的缓冲流的写法:


new BufferedReader(new InputStreamReader(System.in)));


将字节以字符形式输出到控制台的字符缓冲流的写法:


new BufferedWriter( new OutputStreamWriter(System.out))

J. java读取本地文件路径怎么写

Java中获取用户本地路径的方法:
用request对象来获取:request.getRequestURL();
或者用:request.getRequestURI();

热点内容
台式电脑修改密码在哪里修改 发布:2025-02-08 08:25:18 浏览:295
linux编译opencv 发布:2025-02-08 08:14:29 浏览:711
解除先制的密码是多少 发布:2025-02-08 08:10:13 浏览:861
c语言程序设计豆瓣 发布:2025-02-08 08:08:06 浏览:526
学校服务器如何进入密码界面 发布:2025-02-08 08:05:45 浏览:821
UE4源码编译要多久 发布:2025-02-08 07:52:50 浏览:233
java架构师做什么 发布:2025-02-08 07:38:32 浏览:774
java解码器 发布:2025-02-08 07:25:35 浏览:297
p4忘记密码了如何刷机 发布:2025-02-08 07:25:25 浏览:307
java分隔 发布:2025-02-08 07:15:02 浏览:813