导航:首页 > 编程语言 > java读写流

java读写流

发布时间:2022-05-03 00:49:33

java中关于文件流的读写(Writer Reader)

public class BufferedReader extends Reader
{
public BufferedReader (Reader in)//构造方法
public class BufferedReader (Reader in,int sz)//sz 指定字符缓冲区长度
public String readLine()throws IOException//读取一行字符串,输入流结束时返回null
}

public class BufferedWriter extends Writer
{
public BufferedWriter (Writer out)//构造方法
public class BufferedWriter(Writer out,int sz)//sz 指定字符缓冲区长度
public void newLine()throws IOException//写入一个换行符
}

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

㈢ java字符流的读取和写入是什么

输入和输出操作的是字符,写入和读取的类是Writer和Reader,是抽象类,分别对相对应的子类FielWrite和FileReader类
关注我,跟一群朋友一起学习;

㈣ 几种Java读写数据流性能对比

public static int FileOutputStreamTime = 0; 2 public static int BufferedOutputStreamTime = 0; 3 public static int FileWriterTime = 0; 4 public static int FileInputStreamTime = 0; 5 public static int BufferedInputStreamTime = 0; 6 public static int FileReaderTime = 0; 7 8 public static void write(String filePath, String content){ 9 FileOutputStream out = null; 10 FileOutputStream outStr = null; 11 BufferedOutputStream buf = null; 12 FileWriter fw = null; 13 File f = new File(filePath); 14 15 try { 16 //Test FileOutputStream 17 long begin1 = System.currentTimeMillis();
18 out = new FileOutputStream(f); 19 out.write(content.getBytes()); 20 out.close(); 21 long end1 = System.currentTimeMillis(); 22 FileOutputStreamTime += end1 - begin1; 23 24 //Test BufferedOutputStream 25 long begin2 = System.currentTimeMillis(); 26 outStr = new FileOutputStream(f); 27 buf = new BufferedOutputStream(outStr); 28 buf.write(content.getBytes()); 29 buf.flush(); 30 buf.close(); 31 long end2 = System.currentTimeMillis(); 32 BufferedOutputStreamTime += end2 - begin2; 33 34 //Test FileWriter 35 long begin3 = System.currentTimeMillis(); 36 // the second parameter "true",Whether or not a file will be covered 37 // or appended. 38 fw = new FileWriter(f); 39 // fw = new FileWriter("d:/test/testwrite/add2.txt",true); 40 fw.write(content); 41 fw.close(); 42 long end3 = System.currentTimeMillis(); 43 FileWriterTime += end3 - begin3; 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } finally { 47 try { 48 fw.close(); 49 buf.close(); 50 outStr.close(); 51 out.close(); 52 } catch (Exception e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 public static void read(String filePath){ 59 FileInputStream in = null; 60 BufferedInputStream buf = null; 61 FileReader reader = null; 62 BufferedReader br = null; 63 StringBuffer sb = new StringBuffer(); 64 65 try { 66 //Test FileInputStream 67 long begin1 = System.currentTimeMillis(); 68 File f = new File(filePath); 69 in = new FileInputStream(f); 70 int len1 = 512; 71 byte[] bytes1 = new byte[len1]; 72 73 while ((len1 = in.read(bytes1, 0, len1)) != -1) { 74 if(len1 < 512){ 75 byte[] tmpBuf = new byte[len1]; 76 System.array(bytes1, 0, tmpBuf, 0, len1);
77 sb.append(new String(tmpBuf)); 78 tmpBuf = null; 79 }else{ 80 sb.append(new String(bytes1)); 81 } 82 } 83 84 in.close(); 85 long end1 = System.currentTimeMillis(); 86 FileInputStreamTime += end1 - begin1; 87 88 //Test BufferedInputStream 89 long begin2 = System.currentTimeMillis(); 90 int len2 = 512; 91 byte[] bytes2 = new byte[len2]; 92 buf = new BufferedInputStream(new FileInputStream(f)); 93 while ((len2 = buf.read(bytes2, 0, len2)) != -1) { 94 if(len2 < 512){ 95 byte[] tmpBuf = new byte[len2]; 96 System.array(bytes2, 0, tmpBuf, 0, len2);
97 sb.append(new String(tmpBuf)); 98 tmpBuf = null; 99 }else{100 sb.append(new String(bytes2));101 }102 }103 104 buf.close();105 long end2 = System.currentTimeMillis();106 BufferedInputStreamTime += end2 - begin2;107 108 //Test FileReader109 long begin3 = System.currentTimeMillis();110 reader = new FileReader(f);111 br = new BufferedReader(reader);112 String str;113 while ((str = br.readLine()) != null) {114 sb.append(str);115 }116 br.close();117 reader.close();118 long end3 = System.currentTimeMillis();119 FileReaderTime += end3 - begin3;120 121 } catch (Exception e) {122 e.printStackTrace();123 } finally {124 try {125 br.close();126 reader.close();127 in.close();128 buf.close();129 } catch (Exception e) {130 e.printStackTrace();131 }132 }133 }

㈤ java缓冲流读写数据

---下面都是以字节流方式操作---
//读数据:
BufferedInputStreambis=newBufferedInputStream(newFileInputStream("xx.xx"));
byte[]b=newbyte[1024];
intlen=0;
while((len=bis.read(b))!-1){
//这样就读取并输出了,如果是别的文件的话乱码,因为二进制文件
System.out.println(newString(b,0,len));
}
bis.close();//关闭流,节省资源


//写数据:
BufferedOutputStreambos=newBufferedOutputStream(newFileOuputStream("xx.xx"));
//使用缓冲区写二进制字节数据
bos.write("xxxxx".getBytes());
bos.close();//关闭流,节省资源

如果字符流的话就是:

BufferedReader //读取

BufferedWriter //写入

㈥ java 中简述使用流进行读写文本文件的步骤

一、Java IO学习基础之读写文本文件

Java的IO操作都是基于流进行操作的,为了提高读写效率一般需要进行缓冲。

简单的示例程序如下:

/**
* 读出1.txt中的内容,写入2.txt中
*
*/

import java.io.*;

public class ReadWriteFile{
public static void main(String[] args){
try{

File read = new File("c:\\1.txt");
File write = new File("c:\\2.txt");

BufferedReader br = new BufferedReader(
new FileReader(read));
BufferedWriter bw = new BufferedWriter(
new FileWriter(write));
String temp = null;
temp = br.readLine();
while(temp != null){
//写文件
bw.write(temp + "\r\n"); //只适用Windows系统
//继续读文件
temp = br.readLine();
}

bw.close();
br.close();

}catch(FileNotFoundException e){ //文件未找到
System.out.println (e);
}catch(IOException e){
System.out.println (e);
}
}
}

以上是一个比较简单的基础示例。本文上下两部分都是从网上摘抄,合并在一起,方便下自己以后查找。

二、Java IO学习笔记+代码

文件对象的生成和文件的创建

/*
* ProcessFileName.java
*
* Created on 2006年8月22日, 下午3:10
*
* 文件对象的生成和文件的创建
*/
package study.iostudy;
import java.io.*;
public class GenerateFile
{
public static void main(String[] args)
{
File dirObject = new File("d:\\mydir");
File fileObject1 = new File("oneFirst.txt");
File fileObject2 = new File("d:\\mydir", "firstFile.txt");
System.out.println(fileObject2);
try
{
dirObject.mkdir();
}catch(SecurityException e)
{
e.printStackTrace();
}
try
{
fileObject2.createNewFile();
fileObject1.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
}
}

文件名的处理
/*
* ProcessFileName.java
*
* Created on 2006年8月22日, 下午3:29
*
* 文件名的处理
*/
package study.iostudy;
import java.io.*;
/*
* 文件名的处理
* String getName(); 获得文件的名称,不包含文件所在的路径。
* String getPath(); 获得文件的路径。
* String getAbsolutePath(); 获得文件的绝对路径。
* String getParent(); 获得文件的上一级目录的名称。
* String renameTo(File newName); 按参数中给定的完整路径更改当前的文件名。
* int compareTo(File pathName); 按照字典顺序比较两个文件对象的路径。
* boolean isAbsolute(); 测试文件对象的路径是不是绝对路径。
*/
public class ProcesserFileName
{
public static void main(String[] args)
{
File fileObject1 = new File("d:\\mydir\\firstFile.txt");
File fileObject2 = new File("d:\\firstFile.txt");
boolean pathAbsolute = fileObject1.isAbsolute();
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("There are some information of fileObject1's file name:");
System.out.println("fileObject1: " + fileObject1);
System.out.println("fileObject2: " + fileObject2);
System.out.println("file name: " + fileObject1.getName());
System.out.println("file path: " + fileObject1.getPath());
System.out.println("file absolute path: " + fileObject1.getAbsolutePath());
System.out.println("file's parent directory: " + fileObject1.getParent());
System.out.println("file's absoulte path: " + pathAbsolute);
int sameName = fileObject1.compareTo(fileObject2);
System.out.println("fileObject1 compare to fileObject2: " + sameName);
fileObject1.renameTo(fileObject2);
System.out.println("file's new name: " + fileObject1.getName());
}
}

测试和设置文件属性
/*
* SetterFileAttribute.java
*
* Created on 2006年8月22日, 下午3:51
*
* 测试和设置文件属性
*/
package study.iostudy;
import java.io.*;
public class SetterFileAttribute
{
/*
* File类中提供的有关文件属性测试方面的方法有以下几种:
* boolean exists(); 测试当前文件对象指示的文件是否存在。
* boolean isFile(); 测试当前文件对象是不是文件。
* boolean isDirectory(); 测试当前文件对象是不是目录。
* boolean canRead(); 测试当前文件对象是否可读。
* boolean canWrite(); 测试当前文件对象是否可写。
* boolean setReadOnly(); 将当前文件对象设置为只读。
* long length(); 获得当前文件对象的长度。
*/
public static void main(String[] args)
{
File dirObject = new File("d:\\mydir");
File fileObject = new File("d:\\mydir\\firstFile.txt");
try
{
dirObject.mkdir();
fileObject.createNewFile();
}catch(IOException e)
{
e.printStackTrace();
}
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
System.out.println("there are some information about property of file object:");
System.out.println("file object : " + fileObject);
System.out.println("file exist? " + fileObject.exists());
System.out.println("Is a file? " + fileObject.isFile());
System.out.println("Is a directory?" + fileObject.isDirectory());
System.out.println("Can read this file? " + fileObject.canRead());
System.out.println("Can write this fie? " + fileObject.canWrite());
long fileLen = fileObject.length();
System.out.println("file length: " +fileLen);
boolean fileRead = fileObject.setReadOnly();
System.out.println(fileRead);
System.out.println("Can read this file? " + fileObject.canRead());
System.out.println("Can write this fie? " + fileObject.canWrite());
System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * ");
}
}

文件操作方法
/*
* FileOperation.java
*
* Created on 2006年8月22日, 下午4:25
*
* 文件操作方法
*/

package study.iostudy;
import java.io.*;
/*
* 有关文件操作方面的方法有如下几种:
* boolean createNewFile(); 根据当前的文件对象创建一个新的文件。
* boolean mkdir(); 根据当前的文件对象生成一目录,也就是指定路径下的文件夹
* boolean mkdirs(); 也是根据当前的文件对象生成一个目录,
* 不同的地方在于该方法即使创建目录失败,
* 也会成功参数中指定的所有父目录。
* boolean delete(); 删除当前的文件。
* void deleteOnExit(); 当前Java虚拟机终止时删除当前的文件。
* String list(); 列出当前目录下的文件。
*/
public class FileOperation
* 找出一个目录下所有的文件
package study.iostudy;
import java.io.*;
public class SearchFile
{
public static void main(String[] args)
{
File dirObject = new File("D:\\aa");
Filter1 filterObj1 = new Filter1("HTML");
Filter2 filterObj2 = new Filter2("Applet");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("list HTML files in directory: " + dirObject);
String[] filesObj1 = dirObject.list(filterObj1);
for (int i = 0; i < filesObj1.length; i++)
{
File fileObject = new File(dirObject, filesObj1[i]);
System.out.println(((fileObject.isFile())
? "HTML file: " : "sub directory: ") + fileObject);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
String[] filesObj2 = dirObject.list(filterObj2);
for (int i = 0; i < filesObj2.length; i++)
{
File fileObject = new File(dirObject, filesObj2[i]);
System.out.println(((fileObject.isFile())
? "htm file: " : "sub directory: ") + fileObject);
}
}
}

class Filter1 implements FilenameFilter
{
String fileExtent;
Filter1(String extentObj)
{
fileExtent = extentObj;
}

public boolean accept(File dir, String name)
{
return name.endsWith("." + fileExtent);
}
}

class Filter2 implements FilenameFilter
{
String fileName;
Filter2(String fileName)
{
this.fileName = fileName;
字符流处理
* ProcesserCharacterStream.java
* 字符流处理
*
* java.io包中加入了专门用于字符流处理的类,这些类都是Reader和Writer类的子类,
* Reader和Writer是两个抽象类,只提供了一系列用于字符流处理的接口,不能生成这
* 两个类的实例。
* java.io包中用于字符流处理的最基本的类是InputStreamReader和OutputStreamWriter,
* 用来在字节流和字符流之间作为中介。
*
* 下面是InputStreamReader类和OutputStreamWriter类的常用方法:
*
* public InputStreamReader(InputStream in)
* 根据当前平台缺省的编码规范,基于字节流in生成一个输入字符流。
* public InputStreamReader(InputStream in, String sysCode)throws UnSupportedEncodingException
* 按照参数sysCode指定的编码规范,基于字节流in构造输入字符流,如果不支持参数sysCode中指定的编码规范,就会产生异常。
* public OutputStreamWriter(OutputStream out)
* 根据当前平台缺省的编码规范,基于字节流out生成一个输入字符流。
* public OutputStreamWriter(OutputStream out, String sysCode) throws UnsupportedEncodingException
* 按照参数sysCode指定的编码规范,基于字节流out构造输入字符流,如果不支持参数sysCode中指定的编码规范,就会产生异常。
* public String getEncoding()
* 获得当前字符流使用的编码方式。
* public void close() throws IOException
* 用于关闭流。
* public int read() throws IOException
* 用于读取一个字符。
* public int read(char[] cbuf, int off, int len)
* 用于读取len个字符到数组cbuf的索引off处。
* public void write(char[] cbuf, int off, int len) throws IOException
* 将字符数组cbuf中从索引off处开始的len个字符写入输出流。
* public void write(int c) throws IOException
* 将单个字符写入输入流。
* public void write(String str, int off, int len) throws IOException
* 将字符串str中从索引off位置开始的ltn个字符写入输出流。
*
* 此外,为了提高字符流处理的效率,在Java语言中,引入了BufferedReader和BufferWriter类,这两个类对字符流进行块处理。
* 两个类的常用方法如下:
* public BufferedReader(Reader in)
* 用于基于普通字符输入流in生成相应的缓冲流。
* public BufferedReader(Reader in, int bufSize)
* 用于基于普通字符输入流in生成相应的缓冲流,缓冲区大小为参数bufSize指定。
* public BufferedWriter(Writer out)
* 用于基于普通字符输入流out生成相应的缓冲流。
* public BufferedWriter(Writer out, int bufSize)
* 用于基于普通字符输入流out生在相应缓冲流,缓冲流大小为参数bufSize指定。
* public String readLine() throws IOException
* 用于从输入流中读取一行字符。
* public void newLine() throws IOException
* 用于向字符输入流中写入一行结束标记,值得注意的是,该标记不是简单的换行符"\n",而是系统定义的属性line.separator。
在上面的程序中,我们首先声明了FileInputStream类对象inStream和
* FileOutputStream类的对象outStream,接着声明了BufferInputStream
* 类对象bufObj、BufferedOutputStream类对象bufOutObj、
* DataInputStream类对象dataInObj以及PushbackInputStream类对象pushObj,
* 在try代码块中对上面这些对象进行初始化,程序的目的是通过BufferedInputStream
* 类对象bufInObj和BufferedOutputStream类对象bufOutObj将secondFile.txt
* 文件中内容输出到屏幕,并将该文件的内容写入thirdFile.txt文件中,值得注意的是,
* 将secondFile.txt文件中的内容输出之前,程序中使用
* "System.out.println(dataInObj.readBoolean());" 语句根据readBoolean()结果
* 输出了true,而secondFile.txt文件开始内容为“Modify”,和一个字符为M,
* 因此输出的文件内容没有“M”字符,thirdFile.txt文件中也比secondFile.txt
* 文件少第一个字符“M”。随后,通过PushbackInputStream类对象pushObj读取
* thirdFile.txt文件中的内容,输出读到的字符,当读到的不是字符,输出回车,将字符
* 数组pushByte写回到thirdFile.txt文件中,也就是“ok”写回文件中。
* 对象串行化
* 对象通过写出描述自己状态的数值来记录自己,这个过程叫做对象串行化。对象的寿命通
* 常是随着生成该对象的程序的终止而终止,在有些情况下,需要将对象的状态保存下来,然后
* 在必要的时候将对象恢复,值得注意的是,如果变量是另一个对象的引用,则引用的对象也要
* 串行化,串行化是一个递归的过程,可能会涉及到一个复杂树结构的串行化,比如包括原有对
* 象,对象的对象等。
* 在java.io包中,接口Serializable是实现对象串行化的工具,只有实现了Serializable
* 的对象才可以被串行化。Serializable接口中没有任何的方法,当一个类声明实现Seriali-
* zable接口时,只是表明该类遵循串行化协议,而不需要实现任何特殊的方法。
* 在进行对象串行化时,需要注意将串行化的对象和输入、输出流联系起来,首先通过对
* 象输出流将对象状态保存下来,然后通过对象输入流将对象状态恢复。

㈦ java中字节流读写和字符流读写怎么理解

字节流与字符流主要的区别是他们的的处理方式
字节流是最基本的,所有的InputStream和OutputStream的子类都是,主要用在处理二进制数据,它是按字节来处理的
但实际中很多的数据是文本,又提出了字符流的概念,它是按虚拟机的encode来处理,也就是要进行字符集的转化
这两个之间通过 InputStreamReader,OutputStreamWriter来关联,实际上是通过byte[]和String来关联
在实际开发中出现的汉字问题实际上都是在字符流和字节流之间转化不统一而造成的

在从字节流转化为字符流时,实际上就是byte[]转化为String时,
public String(byte bytes[], String charsetName)
有一个关键的参数字符集编码,通常我们都省略了,那系统就用操作系统的lang
而在字符流转化为字节流时,实际上是String转化为byte[]时,
byte[] String.getBytes(String charsetName)
也是一样的道理

㈧ java 文件读写流

读写是两个不同的分支,通常都是分开单独使用的。
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:
OutputStreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

㈨ JAVA语言中向文件的读写中有好多流都搞不懂

记住: 万变不离其宗!
两个最原始最基础的流为 InputStream 和OutputStream

分别用于输入和输出。
缺点是只能读写字节 byte 或 byte[]

所以为了方便大家使用,sun又安排了很多具有高级功能的流
如DataInputStream 等,它们可以读取一些高级的东西,如readUTF(),readLong()等等。
它们其实就是对InputStream进行的封装,来源于这两个原始的流。或继承于它们。

所以你在学习的时候,建议先把最原始的两个流搞清楚! 然后分析问题的时候,想想你需要什么样的流,有什么样的流可以简化你的工作,他们分别比基本流新增了哪些特殊功能。 这样对症下药,楼主你的java功夫就能很快进步

㈩ java中怎么用io流读写文件

importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;

publicclassTest14{
publicstaticvoidmain(String[]args)throwsIOException{
StringfPath="C:/test.txt";
//读
BufferedReaderbr=newBufferedReader(newFileReader(fPath));
System.out.println(br.readLine());
br.close();////使用后记得关闭
//写
BufferedWriterbw=newBufferedWriter(newFileWriter(fPath));
bw.write("写一段话到文件里");
bw.flush();
bw.close();//使用后记得关闭
}
}

阅读全文

与java读写流相关的资料

热点内容
职业生涯pdf 浏览:953
ubuntu安装软件php 浏览:159
黑马程序员退学流程 浏览:362
网页服务器崩溃怎么回事 浏览:651
cnc编程前景怎么样 浏览:319
lniux命令详解 浏览:493
linuxmysql查询日志 浏览:368
老捷达伙伴压缩比 浏览:93
改后缀加密 浏览:433
邮局选址问题算法 浏览:14
河北服务器内存云主机 浏览:12
在电脑上怎么找到加密狗图标 浏览:435
电脑的浏览器怎么打开pdf文件怎么打开 浏览:142
pdf卡片库下载 浏览:11
单片机中二进制表示什么 浏览:725
java网络编程推荐 浏览:795
施耐德开关编程 浏览:66
组织胚胎学pdf 浏览:844
linux查看发包 浏览:496
加密货币交易所暴利时代 浏览:824