java的IO在Android中可以使用的那一部分
字节流可以处理任何文件,若是音乐文件可以把字节流转换成MP3格式,若是图片可以把字节流转换成图片的响应格式(在Android中通常是Bitmap),若处理的是文本文件通常需要转换成字符流,为了提高效率一般会使用带缓冲的字符缓冲流。
处理文本文件,有如下的转换形式:
文件——>字节流
文件——>字节流——>字节缓冲流
文件——>字节流——>字符流
文件——>字节流——>字符流——>字符缓冲流
文件——>字符流——>字符缓冲流
感觉没什么额外的东西可以写的,代码中有详细的注释
public class Main {
static File file = new File("text.txt");
public static void main(String[] args) {
// TODO Auto-generated method stub
// BtyeOutputStream();//字节输出流
// BtyeInputStream();//字节输入流
// BtyeInputOutputStream();//字节输入输出流
// BtyeBufferedInputOutputStream();//字节输入输出缓冲流
// CharStream();//字符输入输出流
// CharBufferedStream();//字符输入输出缓冲流
// FileRW();//读写文件的字符流
// // 随机读
// try {
// RandomAccessFile raf = new RandomAccessFile(file, "r");
// raf.seek(10);
// byte[] str = new byte[30];
// raf.read(str);
// String in = new String(str);
// System.out.println(in);
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//随机写
if (file.exists()) {
file.delete();
}
new WriteFile(file, 5).start();
new WriteFile(file, 3).start();
new WriteFile(file, 1).start();
new WriteFile(file, 4).start();
new WriteFile(file, 2).start();
}
// 字节输出流
private static void BtyeOutputStream() {
try {
// 如果不存在则建立文件,如果已存在则覆盖文件内容,需添加FileNotFoundExecption异常处理
FileOutputStream fos = new FileOutputStream("text.tex");
String outString = "写入磁盘的数据:Hello World1234";
// 字符转化为字节,不加参数会采用本地系统默认的编码方式解码,带参数需加UnsupportedEncodingExecption异常处理
byte output[] = outString.getBytes("UTF-8");
fos.write(output);// 向输出流写入字节数组,需添加IOException异常处理
fos.flush();// 刷新流,强制输出流中内容
fos.close();// 善始善终,关闭流
System.out.println("OK");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 字节输入
private static void BtyeInputStream() {
try {
FileInputStream fis = new FileInputStream("text.txt");// 文件输入流,需加FileNotFoundException异常处理
byte input[] = new byte[100];
fis.read(input);// 从输入流写入字节数组,需加iOException异常处理
// 字节转化为字符,不加第二个参数会采用本地系统默认的编码方式解码,带参数需加UnsupportedEncodingExecption异常处理
String inputString = new String(input, "UTF-8");
System.out.println(inputString);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 字节输入输出流
private static void BtyeInputOutputStream() {
try {
FileInputStream fis = new FileInputStream("text.txt");
FileOutputStream fos = new FileOutputStream("textsub.txt");
byte input[] = new byte[128];
long before = System.currentTimeMillis();
while (fis.read(input) != -1) {
fos.write(input);
}
fos.flush();
fis.close();
fos.close();
System.out.println(System.currentTimeMillis() - before + "ms");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 带缓冲的输入和输出流,明显快于无缓冲的,数据量小不明显
private static void BtyeBufferedInputOutputStream() {
try {
FileInputStream fis = new FileInputStream("text.txt");
// 从字节输入流构建字节输入缓冲流,可加第二个参数自定义缓冲区大小(单位:字节)
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("textsub.txt");
// 从字节输出流构建字节输出缓冲流,可加第二个参数自定义缓冲区大小(单位:字节)
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte input[] = new byte[128];
long before = System.currentTimeMillis();
while (bis.read(input) != -1) {
bos.write(input);
}
bos.flush();
bis.close();// 先开后关
bos.close();
fis.close();
fos.close();
System.out.println(System.currentTimeMillis() - before + "ms");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 字符流
private static void CharStream() {
try {
// 字节流
FileInputStream fis = new FileInputStream("java.txt");
FileOutputStream fos = new FileOutputStream("javasub.txt");
// 字符流
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
char input[] = new char[100];
int l = 0;
while ((l = isr.read(input)) != -1) {
// String inputString = new String(input,0,l);
osw.write(input, 0, l);
}
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("ok");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 字符缓冲流
private static void CharBufferedStream() {
try {
// 构建字节流
FileInputStream fis = new FileInputStream("java.txt");
FileOutputStream fos = new FileOutputStream("javasub.txt");
// 从字节流构建字符流
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
// 从字符流构建字符缓冲流
BufferedReader br = new BufferedReader(isr);
// BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(osw, true);// 专用于文本输出操作
String input;
while ((input = br.readLine()) != null) {
// bw.write(input);
pw.println(input);// 自动刷新
}
br.close();
// bw.flush();bw.close();
pw.close();
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("ok");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 文件读写专用
private static void FileRW() {
try {
// 直接构造字符流
FileReader fr = new FileReader("java.txt");
FileWriter fw = new FileWriter("java_sub.txt");
// 从字符流构建字符缓冲流
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
String line;
while ((line = br.readLine()) != null) {
bw.write(line + "\n");
}
bw.flush();
bw.close();
fw.close();
br.close();
fr.close();
System.out.println("ok");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class WriteFile extends Thread {
File file;
int block;
int L = 100;//块的长度
/**
* File
* |*** |+++
* |-----------***---------+++--------------------------|
* |---
*/
/**
* 1 2 3 4 5(2)
* |------------|------------|------------|------------|------------|
* 0xL 1xL
*
* @param f
* @param b
*/
public WriteFile(File f,int b){
this.file = f;
this.block = b;
}
public void run() {
try {
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek((block-1)*L);
raf.writeBytes("This is block"+block);
for (int i = 0; i < 10; i++) {
raf.writeBytes("-");
}
raf.writeBytes("\n");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}