java几种读写的方式
1、按字节读写文件内容
字节读取
public void testReadByByte() throws IOException
{
//字节读取
String path = "src/test/resources";
File file = new File(path+"/zj.txt");
byte[] bs = new byte[512];
try {
InputStream in = new FileInputStream(file);
in.read(bs);
System.out.println(bs);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
字节写入:
public void testWriteFileByByte(){
//字节写入文件
String path = "src/test/resources";
File file = new File(path+"/zj.txt");
try {
FileOutputStream out = new FileOutputStream(file);
byte b = 97;//assic值
out.write(b);
} catch (IOException e) {
e.printStackTrace();
}
}
2、按字符读写文件内容
字符读取(两种方式)
public void testReadByChar() throws IOException
{
//字符读取,FileReader方法读取
String path = "src/test/resources";
File file = new File(path+"/zj.txt");
char[] cs = new char[5];
Reader reader = new FileReader(file);
try {
reader.read(cs);
System.out.println(new String(cs));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void testReadByChar2() throws IOException
{
//字符读取,InputStreamReader读取
String path = "src/test/resources";
File file = new File(path+"/zj.txt");
char[] cs = new char[5];
Reader reader = new InputStreamReader(new FileInputStream(file));
try {
reader.read(cs);
System.out.println(new String(cs));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
字符写入(两种方式)
public void testWriteFileByChar(){
//字符写入文件,FileWriter的方式
String path = "src/test/resources";
File file = new File(path+"/zj.txt");
try {
FileWriter writer = new FileWriter(file);
String str = "writing string";
writer.write(str);
writer.flush();// 此句不能省
} catch (IOException e) {
e.printStackTrace();
}
}
public void testWriteFileByChar2(){
//字符写入文件,OutputStreamWriter的方式
String path = "zj/src/test/resources";
File file = new File(path+"/zj.txt");
try {
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file));
String str = "writing string";
writer.write(str);
writer.flush();// 此句不能省
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
}
}
3、按行读取文件内容
两种方式:
关键代码
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
public void testReadLine()
{
//逐行读取文件
File file = new File("src/test/resources/zj.txt");
BufferedReader bufferReader;
try {
bufferReader = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String s = null;
while((s = bufferReader.readLine())!=null)
System.out.println(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
4、读写gz文件
读:
关键代码:
BufferedReader bufferReader new BufferedReader(new InputStreamReader(new GZIPInputStream( new FileInputStream(file))));
例子:
public void testReadGzFile()
{
//读取gz文件
String path = "src/test/resources";
File file = new File(path+"/zj.gz");
BufferedReader bufferReader;
try {
bufferReader = new BufferedReader(new InputStreamReader(new GZIPInputStream(
new FileInputStream(file))));
System.out.println(bufferReader.readLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
写:
关键代码:
PrintWriter writer = new PrintWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(filegz))));
示例:
public void testWriteToGzFile2() throws FileNotFoundException{
//写入gz文件,字符方式
String path = "src/test/resources";
File filegz = new File(path+"/zj.gz");
PrintWriter out;
try {
out = new PrintWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(filegz))));
System.out.println("Writing gzfile");
out.write("写gz文件!");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
5、在文件末尾追加字符串
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
/**
*
* @author malik
* @version 2011-3-10 下午10:49:41
*/
public class AppendFile {
public static void method1(String file, String conent) {
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
out.write(conent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用FileWriter
*
* @param fileName
* @param content
*/
public static void method2(String fileName, String content) {
FileWriter writer = null;
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
writer = new FileWriter(fileName, true);
writer.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 追加文件:使用RandomAccessFile
*
* @param fileName 文件名
* @param content 追加的内容
*/
public static void method3(String fileName, String content) {
RandomAccessFile randomFile = null;
try {
// 打开一个随机访问文件流,按读写方式
randomFile = new RandomAccessFile(fileName, "rw");
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
} catch (IOException e) {
e.printStackTrace();
} finally{
if(randomFile != null){
try {
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try{
File file = new File("d://text.txt");
if(file.createNewFile()){
System.out.println("Create file successed");
}
method1("d://text.txt", "123");
method2("d://text.txt", "123");
method3("d://text.txt", "123");
}catch(Exception e){
System.out.println(e);
}
}
}
6、读TXT写gz
public void testReadTxtWriteGZ()
{
File file = new File("src/test/resources/zj.txt");
File filegz = new File("src/test/resources/zj.gz");
PrintWriter writer = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
// writer = new PrintWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(filegz))));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(filegz)))));//快一点
String str =null;
while((str = reader.readLine())!=null)
{
writer.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
writer.close();//writer一定要关闭
}
}
7、 读gz写txt
public void testReadGZWriteTXT()
{
File file = new File("src/test/resources/zj.txt");
File filegz = new File("src/test/resources/zj.gz");
PrintWriter writer = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(filegz))));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))));
String str =null;
while((str = reader.readLine())!=null)
{
writer.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
writer.close();//writer一定要关闭
}
}