java IO
Posted nedrain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java IO相关的知识,希望对你有一定的参考价值。
Introduction
An Example : FileReader
// read the file to console
@Test
public void test2(){
FileReader fr = null;
try {
// step 1 : new the file
File file = new File("/Users/truman/Desktop/a.txt");
// step 2 : provide the IO STREAM
fr = new FileReader(file);
// step 3: read the file
int data = fr.read(); // read() : return a character or -1 if reach the end
while (data != -1) {
System.out.print((char) data);
data = fr.read();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// step 4: close the stream
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Advance on Read()
@Test
public void testAdvancedRead(){
FileReader fr = null;
try {
// I need a file
File file = new File("/Users/truman/Desktop/a.txt");
// create the stream
fr = new FileReader(file);
//****************************************************************
// read
char[] cbuf = new char[5];
int len ;
while ((len = fr.read(cbuf)) != -1){
for(int i = 0; i < len; i++){
System.out.print(cbuf[i]);
}
}
//****************************************************************
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the stream
if(fr != null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter
@Test
public void testWrite(){
FileWriter fw = null;
try {
// step 1: the file you wanna write to
File file = new File("/Users/truman/Desktop/b.txt");
// step 2 : create the writer
fw = new FileWriter(file);
// step 3: write()
fw.write("Hello");
} catch (IOException e) {
e.printStackTrace();
} finally {
// step 4 : close the resource
if(fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Test of FileInputStream & FileOutputStream
@Test
public void testFileCopy(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// the image file
File image1 = new File("/Users/truman/Pictures/Lady.jpg");
File image2 = new File("/Users/truman/Desktop/YoungLady.jpg");
// create the fileInputStream and fileOutputStream
fis = new FileInputStream(image1);
fos = new FileOutputStream(image2);
byte[] bbuf = new byte[1024];
int len;
while ((len = fis.read(bbuf)) != -1){
fos.write(bbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the resources
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
You should use BUFFERED then JUST FILEINPUTSTREAM or something
@Test
public void testBuffered(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File("/Users/truman/Pictures/loveActually.jpg")));
bos = new BufferedOutputStream(new FileOutputStream(new File("/Users/truman/Desktop/love.jpg")));
byte[] bbuf = new byte[1024];
int len;
while ((len = bis.read(bbuf)) != -1){
bos.write(bbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// close the resource
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转换流 InputStreamReader OutputSteamWriter
以上是关于java IO的主要内容,如果未能解决你的问题,请参考以下文章
csharp C#代码片段 - 使类成为Singleton模式。 (C#4.0+)https://heiswayi.github.io/2016/simple-singleton-pattern-us
Android android.view.InflateException Binary XML 文件第 16 行:膨胀类片段时出错