import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import java.io.*;
import java.util.Date;
public class InTest {
public static void main(String[] args)throws IOException {
//init();
// testDataIO();
// testPrintIO();
// testPrintStream();
testObjectIO();
}
private static void init(){
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String cmd = null;
try{
do{
System.out.print("請輸入命令:");
cmd = br.readLine();
cmdReceive(cmd);//接收命令并分發(fā)
//do something
}while(!cmd.equalsIgnoreCase("exit"));
br.close();//關(guān)閉輸入流
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
}
private static void cmdReceive(String cmd){
System.out.println(cmd);
}
private static void testDataIO(){
String path = "data" + File.separator + "int.dat";
File file = new File(path);
DataOutputStream dos = null;
DataInputStream dis = null;
try{
dos = new DataOutputStream(new FileOutputStream(file));
dis = new DataInputStream(new FileInputStream(file));
int readInt;
dos.writeInt(2048);
dos.flush();
dos.close();
readInt = dis.readInt();
dis.close();
System.out.println(readInt);
}catch(FileNotFoundException e){
e.printStackTrace();
System.exit(-1);
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
}
private static void testPrintIO(){
FileWriter fw = null;
PrintWriter pw = null;
String path = "data" + File.separator + "log.log";
try{
pw = new PrintWriter(new FileWriter(path));
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
for(int i=0;i<10000;i++){
pw.print(i + " ");
if(i%100 == 0){
pw.println();
}
}
pw.println("===="+new Date()+"====");
pw.flush();
pw.close();
}
private static void testPrintStream(){
FileOutputStream fis = null;
PrintStream ps = null;
String path = "data" + File.separator + "math.dat";
try{
ps = new PrintStream(new FileOutputStream(path));
ps.write(new byte[]{1,2,3,4,5,6,7,8,9});
}catch(FileNotFoundException e){
e.printStackTrace();
System.exit(-1);
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
ps.flush();
ps.close();
}
private static void testObjectIO(){
String path = "data" + File.separator + "t.obj";
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
ByteArrayOutputStream baos = null;
TT tt = new TT(123,456,"HelloWorld");
try{
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(tt);
oos.writeObject(new TT(666,888,"建國70周年"));
oos.flush();
oos.close();
ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
System.out.println(ois.readObject());
System.out.println(ois.readObject());
// System.out.println(ois.readObject());
ois.close();
}catch(FileNotFoundException e){
e.printStackTrace();
System.exit(-1);
}catch(ClassNotFoundException e){
e.printStackTrace();
System.exit(-1);
}catch (IOException e){
e.printStackTrace();
System.exit(-1);
}
}
}
class TT implements Serializable{ //序列化標(biāo)識接口
int a;
int b;
String s;
transient int k; //transient 修飾的成員序列化不被加入
TT(int a, int b, String s) {
this.a = a;
this.b = b;
this.s = s;
this.k = 15;
}
@Override
public String toString() {
return this.a + " " + this.b + " " + this.s + " " + this.k;
}
}
import java.io.*;
import java.util.ArrayList;
public class IOStreamTest {
public static void main(String[] args){
/*FileInputStream fis = null;
String dir = "data" + File.separator + "kkk.txt";
try{
fis = new FileInputStream(dir);
}catch(FileNotFoundException e){
System.out.println("File Not Found");
System.exit(-1);
}
try{
long sum = 0;
for( int b = 0;(b = fis.read()) != -1;sum++){
System.out.print((char)b);
}
System.out.println();
System.out.println("共讀取了" + sum + "個字符");
fis.close(); //關(guān)閉文件輸入流
}catch(IOException e){
System.out.println("文件讀取錯誤");
System.exit(-1);
}
*/
/* FileReader fr = null;
String dir = "data" + File.separator + "kkkk.txt";
try{
fr = new FileReader(dir);
}catch(IOException e){
// System.out.println("File Not Found");
e.printStackTrace();
System.exit(-1);
}
try{
long sum = 0;
for( int b = 0;(b = fr.read()) != -1;sum++){
System.out.print((char)b);
}
System.out.println();
System.out.println("共讀取了" + sum + "個字符");
fr.close(); //關(guān)閉文件輸入流
}catch(IOException e){
System.out.println("文件讀取錯誤");
System.exit(-1);
}*/
String srcFileName = "data" + File.separator + "1.png";
String descFileName = "data" + File.separator + "3.png";
fileCopy(srcFileName,descFileName);
// String path = "data" + File.separator + "words.txt";
// writeWords(path);
}
private static void writeWords(String path){
// FileWriter fw = null;
BufferedWriter bw = null;
try{
bw = new BufferedWriter(new FileWriter(path));
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
try{
for(int i=0;i<65535;i++){
bw.write(i);
}
bw.flush();
bw.close();
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
}
private static void fileCopy(String srcFileName,String descFileName){
FileInputStream fis = null;
FileOutputStream fos = null;
File file = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// BufferedInputStream bis = null;
// BufferedOutputStream bos = null;
long fileSize=0;
try{
// fis = new FileInputStream(srcFileName);
// fos = new FileOutputStream(descFileName);
file = new File(srcFileName);
fileSize = file.length();
// fileData = new byte[(int)fileSize];
fis = new FileInputStream(srcFileName);
fos = new FileOutputStream(descFileName);
// bis = new BufferedInputStream(new FileInputStream(file));
// bos = new BufferedOutputStream(new FileOutputStream(descFileName));
// System.out.println(fileSize);
}catch(FileNotFoundException e){
e.printStackTrace();
System.exit(-1);
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
try{
int count=0;//存放每次寫出的字節(jié)
long size=0;//累計寫出字節(jié)
byte percent=0;//存放計算百分比
byte[] b = new byte[1024];
long startTime = System.nanoTime();
while((count = fis.read(b,0,b.length)) >= 0){
//size < fileSize &&
baos.write(b,0,count);
// fos.write(fileData,(int)size,count);
size += count; //累計寫出多少字節(jié)
if(percent != (byte)((double)size/fileSize*100)){//限制同一數(shù)值百分比多次出現(xiàn)
percent = (byte)((double)size/fileSize*100);
System.out.println(percent + "%");
}
}
System.out.println("復(fù)制所耗時間:"+(System.nanoTime()-startTime));
// fos.write(fileData); //將內(nèi)存緩存寫到文件
// fos.write(baos.toByteArray());
baos.writeTo(fos);
baos.flush();
baos.close();
fis.close();//關(guān)閉讀入流
fos.flush();//更新緩沖區(qū)
fos.close();//關(guān)閉輸出流
}catch(IOException e){
e.printStackTrace();
System.exit(-1);
}
}
}
//不使用 BufferedIO所耗時間 復(fù)制所耗時間:64342480479
//使用 BufferedIO所耗時間 復(fù)制所耗時間:49165378487
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。