java基础之(一) Io操作 字节和字符


package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileIo {
private static SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss S");
/**
* 目标文件路径
*/
public static final String SAVE_PATH = "d://student.txt";
/**
* 复制文件路径 字节
*/
public static final String COPY_PATH = "d://byte.txt";

/**
* 字节流操作 查询数据库,将数据写入文件。
*
* @throws Exception
*/
public static void stream(String savePath) throws Exception {
// 计算运行时间

long startTime = System.currentTimeMillis();
System.out.println("字节流操作--读取数据库开始时间:"
+ sdf.format(new Date(startTime)));

File file = new File(savePath);
OutputStream ops = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(ops);
String sql = "select * from student";
Connection conn = DbUtils.getConn();
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql);
// 获取表列名
ResultSetMetaData metaData = rs.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
bos.write(metaData.getColumnLabel(i).getBytes());
bos.write("\t".getBytes());
}
bos.write("\r\n".getBytes());

// 读取数据
while (rs.next()) {
bos.write(rs.getBytes(1));
bos.write("\t".getBytes());
bos.write(rs.getBytes(2));
bos.write("\t".getBytes());
bos.write(rs.getBytes(3));
bos.write("\t".getBytes());
bos.write(rs.getBytes(4));
bos.write("\t".getBytes());
bos.write(rs.getBytes(5));
bos.write("\t".getBytes());
bos.write(rs.getBytes(6));
bos.write("\t".getBytes());
bos.write(rs.getBytes(7));
bos.write("\t".getBytes());
bos.write(rs.getBytes(8));
bos.write("\t".getBytes());
bos.write("\r\n".getBytes());
}
bos.flush();
ops.close();
bos.close();
// 计算结束时间
long endTime = System.currentTimeMillis();
System.out.println("字节流操作--读取数据库结束时间:" + sdf.format(new Date(endTime)));
}

/**
* 字节流操作 复制文件
*
* @param savePath
* @param copyPath
* @throws Exception
*/
public static void copy(String savePath, String copyPath) throws Exception {
long startTime = System.currentTimeMillis();
System.out.println("字节流操作--复制开始时间:" + sdf.format(new Date(startTime)));
File file = new File(savePath);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
File outFile = new File(copyPath);
OutputStream ops = new FileOutputStream(outFile);
BufferedOutputStream bos = new BufferedOutputStream(ops);
byte[] b = new byte[1024];
while (bis.read(b) != -1) {

bos.write(b);
}
bos.flush();
ops.close();
bos.close();
fis.close();
bis.close();
} else {
System.out.println("文件不存在");
}
long endTime = System.currentTimeMillis();
System.out.println("字节流操作--复制结束时间:" + sdf.format(new Date(endTime)));
}

/**
* 字符操作 复制文件夹
* @param savePath
* @param copyPath
* @throws Exception
*/
public static void filecopy(File sourceFile, File targetFile)
throws Exception {
if(!targetFile.exists()){
targetFile.mkdir();
}
File tarpath = new File(targetFile, sourceFile.getName());

if (sourceFile.isDirectory()) {
tarpath.mkdir();
File[] dir = sourceFile.listFiles();
for (int i = 0; i < dir.length; i++) {
filecopy(dir[i], tarpath);
}
} else {
Reader reader = null;
Writer writer = null;
try {
reader = new FileReader(sourceFile);
writer = new FileWriter(tarpath);

char[] buf = new char[1024];
while (reader.read(buf) != -1) {
writer.write(buf);
}
} finally {

reader.close();
writer.close();
}
}

}

/**
* @param args
*/
public static void main(String[] args) throws Exception {
// FileIo.stream(SAVE_PATH);
// FileIo.copy(SAVE_PATH, COPY_PATH);

FileIo.filecopy(new File("E://java"), new File("E://新建文件夹"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值