JAVASE入门十二脚-IO流charArrayReader,bufferedReader,输入与输出,采集百度网页,分块操作
charArrayReader
package Reader_Wirter;
import java.io.CharArrayReader;
import java.io.IOException;
public class charArraySreaminput {
public static void main(String[] args) {
char[] chars="大王叫我来寻山".toCharArray();
CharArrayReader charArrayReader=new CharArrayReader(chars);
try {
int length=0;
while((length=charArrayReader.read())!=-1){
System.out.print((char)length);}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package Reader_Wirter;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BufferReader {
public static void main(String[] args) {
BufferedReader bufferedReader=null;
try {
bufferedReader=new BufferedReader(new FileReader("aaa.txt"));
String read=null;
while ((read=bufferedReader.readLine())!=null){
System.out.println(read);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package Reader_Wirter;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args) {
FileWriter fileWriter=null;
BufferedWriter bufferedWriter=null;
try {
fileWriter=new FileWriter(new File("abc.txt"));
bufferedWriter=new BufferedWriter(fileWriter);
bufferedWriter.write("da大大大");
bufferedWriter.newLine();
bufferedWriter.append("ww.coom");
bufferedWriter.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
输入与输出
bufferedreader.readline==null字符串
package Reader_Wirter;
import java.io.*;
import java.util.NavigableMap;
public class BufferedReader_Wirter {
public static void main(String[] args) {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(System.out);
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
String str = "";
try {
while (!str.equals("exit")) {
str = bufferedReader.readLine();
bufferedWriter.write(str);
bufferedWriter.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStreamReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
outputStreamWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
采取百度网页内容
package Reader_Wirter;
import com.sun.org.apache.xpath.internal.operations.Neg;
import java.io.*;
import java.net.URL;
public class Baidu {
public static void main(String[] args) {
BufferedReader bufferedReader=null;
BufferedWriter bufferedWriter=null;
try {
bufferedReader=new BufferedReader (new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
bufferedWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("baidu.html")));
String msg=null;
while ((msg=bufferedReader.readLine())!=null){
bufferedWriter.write(msg);
bufferedWriter.newLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
bufferedWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
PrintStream
package Reader_Wirter;
import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput;
import java.io.IOException;
import java.io.PrintStream;
public class PrintStreamDemo {
public static void main(String[] args) {
PrintStream printStream=null;
printStream=new PrintStream(System.out);
try {
printStream.write("hello test".getBytes());
printStream.println(123);
//格式化输出
System.out.printf("%s---%d---%f","abc",123,45.25);
System.err.println("daaaa");
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
printStream.close();
}
}
}
分块操作
package Reader_Wirter;
import java.io.*;
/*
* 文件进行分块操作
* */
public class BlockingDemo {
public static void main(String[] args) {
File file=new File("ccc.txt");
//整个文件的大小length
long length = file.length();
//规定快的大小blocksize
int blockSize=1024;
//文件可以分成多少块size
int size=(int)Math.ceil(length*1.0/blockSize);
System.out.printf("要被切成%d个快",size);
int beginPos=0;
int actualSize=(int)(blockSize>length?length:blockSize);
for(int i=0;i<size;i++){
//每次读取快的时候的起始偏移量
beginPos=i*blockSize;
if(i==size-1){
actualSize=(int)length;
}else {
actualSize=blockSize;
length-=actualSize;
}
System.out.println(i+"--->>起始位置:"+beginPos+"----->>读取的大小是"+actualSize);
readSplit(i,beginPos,actualSize);
}
}
public static void readSplit(int i,int beginPos,int actualSize){
RandomAccessFile randomAccessFile=null;
try {
randomAccessFile=new RandomAccessFile(new File("ccc.txt"),"r");
byte[] bytes=new byte[1024];
int length=0;
while ((length=randomAccessFile.read(bytes))!=-1){
if(actualSize>length){
System.out.println(new String(bytes,0,length));
actualSize-=length;
}else {
System.out.println(new String(bytes,0,actualSize));
break;
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
randomAccessFile.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}