JAva入出力ストリーム
7180 ワード
/**
*
*/
package com.XXX.stream;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;
import org.apache.hadoop.io.ArrayFile.Reader;
/**
* @author
* ,
* , ,
* , 。
*/
public class StringByteStream {
private static String mFilePath;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
initialize();
// OutPutStream();
// InPutStream();
// FileWriter();
// FileReader();
FileOutputStream();
// FileInputStream();
// ByteToStringInNoBuff();
// ByteToStringInByBuff();
}
/**
* InputStreamReader ,
* */
private static void FileInputStream() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
StringBuilder mBuilder = new StringBuilder();
try {
FileInputStream mFInputStream = new FileInputStream(mFile);
InputStreamReader mInputStreamReader = new InputStreamReader(mFInputStream);
BufferedReader mReader = new BufferedReader(mInputStreamReader);
String str = null;
while ((str = mReader.readLine()) != null) {
mBuilder.append(str);
}
mFInputStream.close();
mInputStreamReader.close();
// byte[] b = new byte[2048];
// int len = mFInputStream.read();
// mFInputStream.read(b, 0, len);
// String str = new String(b);
// mFInputStream.close();
System.out.println("mBuilder = " + mBuilder);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* FileOutputStream ,
* */
private static void FileOutputStream() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
try {
FileOutputStream mFileOutputStream = new FileOutputStream(mFile);
String str = " ";
byte [] b = new byte[2048];
b = str.getBytes();
mFileOutputStream.flush();
mFileOutputStream.write(b);
mFileOutputStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* ,
* */
private static void ByteToStringInByBuff() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
try {
InputStream mInputStream = new FileInputStream(mFile);
InputStreamReader mInputStreamReader = new InputStreamReader(mInputStream);
BufferedReader mBufferedReader = new BufferedReader(mInputStreamReader);
String outputByBuffer = null;
// outputByBuffer = mBufferedReader.read(); int
while ((outputByBuffer = mBufferedReader.readLine()) != null) {
System.out.println(" = " + outputByBuffer);
}
mInputStreamReader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* , ,
* */
private static void ByteToStringInNoBuff() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
try {
InputStream mInputStream = new FileInputStream(mFile);
InputStreamReader mInputStreamReader = new InputStreamReader(mInputStream);
char [] cha = new char [1024];
int length = mInputStreamReader.read(cha);
String srt = new String(cha);
mInputStreamReader.close();
System.out.println(" = " + srt);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* */
private static void OutPutStream() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);//1. file
OutputStream mOutputStream = null;
try {//2.
mOutputStream = new FileOutputStream(mFile, false);//false
String str = " ";
byte b[] = str.getBytes();//3.
mOutputStream.flush();
mOutputStream.write(b);//4.
mOutputStream.close();//5.
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* *
* */
private static void InPutStream() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
InputStream mInputStream = null;
try {
mInputStream = new FileInputStream(mFile);
byte[] cha = new byte[1024];
int length = mInputStream.read(cha);
mInputStream.read(cha, 0, length);
String str = new String(cha);
mInputStream.close();
System.out.println("str = " + str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* , ,
* FileWriter
* */
private static void FileWriter() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
FileWriter mFileWriter = null;
try {// ,
mFileWriter = new FileWriter(mFile, false);
String str = " ";
mFileWriter.write(str);
mFileWriter.flush();//
mFileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* */
private static void FileReader() {
// TODO Auto-generated method stub
File mFile = new File(mFilePath);
try {
FileReader mFileReader = new FileReader(mFile);
char [] cha = new char[1024];
int length = mFileReader.read(cha);
mFileReader.read(cha, 0, length);
String str = new String(cha);
mFileReader.close();
System.out.println("str = " + str);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void initialize() {
// TODO Auto-generated method stub
mFilePath = "D:\\usersDate\\testStream.txt";
}
}