「java分流读写」java字节流读写文件
今天给各位分享java分流读写的知识,其中也会对java字节流读写文件进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java中怎么用io流读写文件
- 2、用java分别以字节流和文本流方式实现文件的读写操作(先向test1.txt文件中写“各位同学:
- 3、java 文件读写流
- 4、java分流读取数据库里的数据到txt,避免数据量太大读不了报内存溢出的错误
- 5、java 中简述使用流进行读写文本文件的步骤?
- 6、java什么是读写分离
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分别以字节流和文本流方式实现文件的读写操作(先向test1.txt文件中写“各位同学:
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class StreamTest {
public static void main(String[] args) {
StreamTest st = new StreamTest();
String writeStr = "Hello World!\r\n你好!";
String fileName = "outFile.txt";
st.OutputTest(fileName,writeStr);
st.InputTest(fileName);
}
//字节-读
private void InputTest(String fileName) {
File f = createFile(fileName);
FileInputStream fis;
byte[] b = new byte[100];
try {
System.out.println("创建输入流...");
fis = new FileInputStream(f);
System.out.println("创建输入流完成");
System.out.println("开始读取...");
fis.read(b);
System.out.println("读取完成");
String str = new String(b);
System.out.println("读取内容输出:\n"+str);
fis.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到");
}catch(IOException e) {
System.out.println("读取失败");
}
}
//字节-写
private void OutputTest(String fileName,String text) {
File f = createFile(fileName);
FileOutputStream fos;
try{
System.out.println("创建输出流...");
fos = new FileOutputStream(f);
System.out.println("创建输出流完成");
byte[] testBArray = text.getBytes();
System.out.println("开始写数据...");
fos.write(testBArray);
fos.flush();
System.out.println("写数据完成");
fos.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
//创建文件
private File createFile(String fileName) {
File f = new File(fileName);
if(!f.exists()) {
System.out.println("文件不存在");
try{
System.out.println("创建文件...");
f.createNewFile();
System.out.println("创建文件完成");
}catch(IOException e) {
System.out.println("文件创建失败");
e.printStackTrace();
}
}else {
System.out.println("文件已经存在");
}
return f;
}
}
字符流的话改成FileWriter(),FileReader()就好啦!
不懂加:百度HI!^0^
java 文件读写流
首先你要知道java的io流主要分两种,一种是字符流,另一种字节流,还有一种过滤流,这个不常用,暂且可以忽略。
等你这些都掌握了,推荐你用nio包中的管道流。
流的套用可以提升读写效率(这种方式只能是同类流的套用,比如字节流套用字节流),还有一种是字符流与字节流互相转换,转换通过一种叫做“桥转换”的类,比如OutputStreamWriter类。
下面举个最基础的字节流例子:
public void copyFile(String file, String bak) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
byte[] bytes = new byte[1024];
bis = new BufferedInputStream(new FileInputStream(file));//BufferedInputStream会构造一个背部缓冲区数组,将FileInputStream中的数据存放在缓冲区中,提升了读取的性能
bos = new BufferedOutputStream(new FileOutputStream(bak));//同理
int length = bis.read(bytes);
while (length != -1) {
System.out.println("length: " + length);
bos.write(bytes, 0, length);
length = bis.read(bytes);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
字符流的用法:
FileReader fr = new FileReader("D:\\test.txt");
BufferedReader br = new BufferedReader(fr);
或者PrintWriter pw = new PrintWriter(new FileWriter("D:\\test.txt"));
...
java分流读取数据库里的数据到txt,避免数据量太大读不了报内存溢出的错误
2个要点要分清楚,是数据库报错,还是JAVA程序。2种对应不同方法。
拿ORACLE来说。判断是数据库报错,可以让DBA调整数据库参数或者调整JDBC参数。
假设是JAVA程序溢出。首先要注意不要一次读取所有记录存到变量中再写入到TXT。
最好根据ORACLE ROWID分页,一次读取100行或者500行再写入。
java 中简述使用流进行读写文本文件的步骤?
InputStream
三个基本的读方法
abstract int read() : 读取一个字节数据,并返回读到的数据,如果返回-1,表示读到了输入流的末尾。
int read(byte[] b) : 将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。
int read(byte[] b, int off, int len) :将数据读入一个字节数组,同时返回实际读取的字节数。如果返回-1,表示读到了输入流的末尾。off指定在数组b中存放数据的起始偏移位置;len指定读取的最大字节数。
OutputStream
三个基本的写方法
abstract void write(int b) :往输出流中写入一个字节。
void write(byte[] b) :往输出流中写入数组b中的所有字节。
void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据。
其它方法
void flush() :刷新输出流,强制缓冲区中的输出字节被写出。
void close() :关闭输出流,释放和这个流相关的系统资源。
java什么是读写分离
原话叫,读写分离主从同步java 读写分离可以用两条线程做到,一条线程读,一条线程写,应该有个同步容器扮演存储仓库的角色,比如说 就是集合经过改造的,线程还得上锁,大概就是这么个情况,采纳我谢谢
java分流读写的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java字节流读写文件、java分流读写的信息别忘了在本站进行查找喔。