您好,登錄后才能下訂單哦!
java中的io流分為兩類,字符和字節:
class useByteStream
{
/**
* 使用文件輸出字節流
*
*/
public static void testFileOutputStream()
{
OutputStream out = null;
try
{
File f = new File(".\\log\\test.txt");
//out = new FileOutputStream(f);
out = new FileOutputStream(f,true); //追加方式記錄到文件
String str = "Hello World!!!";
byte b[] = str.getBytes();
out.write(b);
out.close();
}
catch(FileNotFoundException e)
{
}
catch(IOException e)
{
}
}
/**
* 使用文件輸入字節流
*/
public static void testFileInputStream()
{
InputStream out = null;
try
{
File f = new File(".\\log\\test.txt");
out = new FileInputStream(f);
String str = "Hello World!!!";
byte b[] = new byte[1000];
int len = out.read(b);
System.out.println(new String(b,0, len) );
out.close();
}
catch(FileNotFoundException e)
{
}
catch(IOException e)
{
}
}
};
class useCharStream
{
/**
* 使用文件字符輸出流
*/
public static void testFileWriter()
{
Writer w = null;
try
{
File f = new File(".\\log\\test2.txt");
w = new FileWriter(f,true); //追加方式
w.write("hello world\r\n");
w.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
/**
* 使用文件字符輸入流
*/
public static void testFileReader()
{
Reader w = null;
try
{
File f = new File(".\\log\\test2.txt");
w = new FileReader(f);
char c[] = new char[1024];
w.read(c);
System.out.println(c);
w.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
};
以上都是基于文件流操作,接下來是基于內存操作流,如果只是寫業務代碼應該很少會用到。
class useMemoryStream
{
/**
* 使用內存操作流,字節
*/
public static void testByteArray()
{
String str = "Hello world";
ByteArrayInputStream bis = null;
ByteArrayOutputStream bos = null;
bis = new ByteArrayInputStream(str.getBytes());
bos = new ByteArrayOutputStream();
int temp =0;
while((temp=bis.read())!=-1)
{
char c = (char)temp;
bos.write(Character.toUpperCase(c));
}
String newStr = bos.toString();
try
{
bis.close();
bos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println(newStr);
}
};
class Send implements Runnable
{
private PipedOutputStream pos = null;
public Send()
{
this.pos = new PipedOutputStream();
}
public void run()
{
String str = "Hello world!!!";
try
{
try
{
Thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
this.pos.write(str.getBytes());
System.out.println("thread:"+Thread.currentThread().getId()+",Send string:"+str);
}
catch(IOException e)
{
e.printStackTrace();
}
try
{
this.pos.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public PipedOutputStream getPos()
{
return this.pos;
}
};
class Receive implements Runnable
{
private PipedInputStream pis = null;
public Receive()
{
this.pis = new PipedInputStream();
}
public void run()
{
byte b[] = new byte[1024];
int len =0;
try
{
len = this.pis.read(b); //阻塞方式
}
catch(IOException e)
{
e.printStackTrace();
}
try
{
pis.close();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("thread:"+Thread.currentThread().getId()+",receive:"+new String(b,0,len));
}
public PipedInputStream getPis()
{
return this.pis;
}
};
class pipedTest
{
public void pipedStream()
{
Send s = new Send();
Receive r = new Receive();
try {
s.getPos().connect(r.getPis());
}
catch(IOException e)
{
e.printStackTrace();
}
new Thread(r).start();
new Thread(s).start();
}
};
以上都是無緩存的,考慮到一般場景下,提高使用性能,最好使用有緩存的字符流:BufferedReader和BufferedWriter。
BufferedReader
只能接受輸入為字符流,不能為字節流。所以有時候會使用InputStreamReader來轉換字節流給字符流使用。還有BufferedWriter
class useBuffer
{
public static void testBufferReader()
{
BufferedReader buf = null;
//此處用到了字節流轉字符流的類InputStreamReader,這是因為BufferedReader只能接收字符流
buf = new BufferedReader(new InputStreamReader(System.in));
String str =null;
try
{
str = buf.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("輸出的內容為:"+str);
}
public static void testBufferWriter()
{
File f = new File(".\\log\\test2.txt");
try {
//默認緩沖區大小 8K 可以通過 new BufferedWriter(new FileWriter(f),1024);指定大小為1K
BufferedWriter out =new BufferedWriter(new FileWriter(f));
out.write("123321123355555", 0, 10);
out.write("\r\n");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
};
SCanner類,輸入數據類。
使用方法和BufferedReader類類似,并且方便驗證數據類型。
class useScan
{
public static void testScan()
{
Scanner scan = new Scanner(System.in);
//以回車作為輸入的結束符號,否則默認是空格
scan.useDelimiter("\r\n");
if(scan.hasNextInt()==true)
{
int str = scan.nextInt();
System.out.println("int "+str);
}
else
{
String str = scan.next();
System.out.println("string "+str);
}
}
};
scan.hasNext
支持正則表達式。比如 hasNext("^\\d{4}-\\d{2}-\\d{2}$")
就是日期格式yyyy-MM-dd的正則表達式,通過next("^\\d{4}-\\d{2}-\\d{2}$")
。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。