「java将文件流写出」java字符流写文件

博主:adminadmin 2022-12-02 01:59:06 54

本篇文章给大家谈谈java将文件流写出,以及java字符流写文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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

你好 我刚刚做了个例子 方便你看

class_writer class 这个例子是从 一个文件中读取数据然后插入了数据库中 你可以只看读取与插入的过程 希望能帮到你.

public class writer {

public boolean writ(String str) {

boolean success=false;

str = str.replaceAll(" ", "").replaceAll("\"", "");

String[] strs = str.split(",");

BaseJDBC base = new BaseJDBC();

String ML = "";

Statement stmt;

Connection conn;

ML = "'"+strs[0].replace(" ", "").trim() + "','"

+ strs[1].replace(" ", "").trim() + "','"

+ strs[2].replace(" ", "").trim() + "','"

+ strs[3].replace(" ", "").trim() + "','"

+ strs[4].replace(" ", "").trim()+ "','"

+ strs[5].replace(" ", "").trim()+"','"

+ strs[6].replace(" ", "").trim()+"'";

String query = "INSERT INTO BANK_INFO VALUES(" + ML + ")";

if (!strs[0].equals("参与者行号")) {

try {

conn=base.genConn();

stmt = conn.createStatement();

int num=stmt.executeUpdate(query);

if(num==1)success=true;

stmt.close();

conn.close();

System.out.println();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println(e.getMessage());

}

}

return success;

}

//class readingclass

public class reading {

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

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

String tempString = null;

int line = 1;

while ((tempString = reader.readLine()) != null) {

writer w=new writer();

try {

tempString.getBytes("utf-8");

} catch (UnsupportedEncodingException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

if(!w.writ(tempString)){

System.out.println("第"+line+"行出现异常:"+tempString);

}else{

System.out.println("第"+line+"行初始化成功!");

}

line++;

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

//1927

public static void main(String[] args) {

// TODO Auto-generated method stub

String fileName = "D:\\BankInfo_20110714094211.csv";

readFileByLines(fileName);

}

java文件流怎么写

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

private File fileObject;

private String savePath;

FileOutputStream fileOutputStream = new FileOutputStream("savePath");

FileInputStream fileInputStream = new FileInputStream(fileObject);

byte[] buffer = new byte[100];

int len = 0;

while ((len = fileInputStream.read(buffer)) 0) {

fileOutputStream.write(buffer, 0, len);

}

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将获得的文件写入输出流

可以通过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);//原样输出读到的内容

};

备注: 上面的bre就是提问者需要的流。流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。

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

The End

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