「javaio读取文件」Java文件读取

博主:adminadmin 2022-11-25 23:23:07 99

今天给各位分享javaio读取文件的知识,其中也会对Java文件读取进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

关于java IO读取文件路径问题

看你的包名,和你的绝对路径,你的aa.txt和bb.txt文件应该是放在包test.io.TestTxt下吧,和TestOneToOnetxt类一个路径,那么你可以这么做:

File f1 = new File(TestOneToOnetxt.class.getResource("aa.txt").getFile());

java中怎么用io流读写文件

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

public class Test14 {

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

        String fPath = "C:/test.txt";

        // 读

        BufferedReader br = new BufferedReader(new FileReader(fPath));

        System.out.println(br.readLine());

        br.close();// // 使用后记得关闭

        // 写

        BufferedWriter bw = new BufferedWriter(new FileWriter(fPath));

        bw.write("写一段话到文件里");

        bw.flush();

        bw.close();// 使用后记得关闭

    }

}

java io读取文件名字

代码如下:

/**

* 文件读写

*/

public void addLogger(String fileName) {

String line = "";

File file = new File("E://tkmFile//knlgfile//template//template.txt");

BufferedReader reader = null;

try {

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

} catch (FileNotFoundException e) {

e.printStackTrace();

}

try {

while (reader.read() != -1) {

line += reader.readLine();

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (reader != null) {

reader.close();

reader = null;

}

} catch (IOException e) {

e.printStackTrace();

}

}

line = " " + line.substring(0, line.lastIndexOf("]"));

line += ",[/"750/", /"" + fileName + "/", /"" + fileName + "/"]" + "];";

System.out.println(line);

FileWriter fw = null;

try {

fw = new FileWriter(Constants.templatePath);

fw.write(line);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (fw != null) {

fw.close();

fw = null;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

java怎么使用io流读取一个文件夹里面

使用工具类Properties

工具类有Load()方法 用于加载文件

首先将文件写成流(输入)

File file=new  File(confPath);

            in = new FileInputStream(file);

加载流load(in)如果需要操作则完成具体操作

输出流并保存文件

out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");

            cp.store(out2);

完成

具体实例代码

public String updateConfParam(ConfParam cpl) throws IOException {

        String error = null;

        Properties cp=new Properties();

        InputStream in= null;

        OutputStreamWriter out1=null;

        OutputStreamWriter out2=null;

        JSONObject jObj = new JSONObject();

        try {

            String confPath=validateSystem(cpl.getConfAddress()+"/"+cpl.getConfName());

            File file=new  File(confPath);

            in = new FileInputStream(file);   

            cp.load(in);

            out1=new OutputStreamWriter(new FileOutputStream(confPath+".bak"),"GBK");

            cp.store(out1);

            cpl.setParaOldValue(cp.getProperty(cpl.getParaKey()));

            cp.setProperty(cpl.getParaKey(), cpl.getParaValue());    

            out2 = new OutputStreamWriter(new FileOutputStream(confPath),"GBK");

            cp.store(out2);

            jObj.put("paraOldValue", cpl.getParaOldValue());

            jObj.put("paraValue", cpl.getParaValue());

        } catch (FileNotFoundException e) {

            error=e.getMessage();

        } catch (IOException e1) {

            error = e1.getMessage();

        }

        finally{

            if(in !=null){

                in.close();

            }

            if(out1 !=null){

                out1.close();

            }

            if(out2 !=null){

                out2.close();

            }

            if(error != null){

                jObj.put("error", error);

            }

        }

        return jObj.toString();

    }

java用IO流一行行的读取文件

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

class ThreadDemo{

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

BufferedReader fr=new BufferedReader(new FileReader("a.txt"));//a.txt代表文件

String line=null;

while((line=fr.readLine())!=null){

System.out.println(line);

}

}

}

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类的对象序列化到磁盘

创建Person类

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

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

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

class Person implements Serializable{

public Person(){}

}

2.创建对象流对象

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

ObjectOutputStream oos =new ObjectOutputStream(

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

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

ListPerson 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))

javaio读取文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于Java文件读取、javaio读取文件的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。