2年ぶりにブログを拾いました
20812 ワード
前回ブログを書いたのは2年前で、大学1年生が終わる頃だったのを覚えています.せっかく続けてきたブログを无情に舍ててしまったのだから、恨まないでほしい.
今すでに大学4年生になって、履歴書を投げて、仕事を探す段階で、一部の学友はすでに仕事を見つけて、まだ一部の学友はまだ始まっていないで、自分はすでに履歴書を投げ始めて、ここで自分が良い仕事を探すことを祈ります.
以前書いたブログはC++を勉強していた時に書いた(習ったことは全部先生に返したようで、先生はいつ授業料を返してくれるか分からない)、最後の技術ブログはIO流についてだったので、後でJava IO流のメモを添付して、亡くなったC++を記念します(でも、必ずもう一度探して帰ります).
メモ:
今すでに大学4年生になって、履歴書を投げて、仕事を探す段階で、一部の学友はすでに仕事を見つけて、まだ一部の学友はまだ始まっていないで、自分はすでに履歴書を投げ始めて、ここで自分が良い仕事を探すことを祈ります.
以前書いたブログはC++を勉強していた時に書いた(習ったことは全部先生に返したようで、先生はいつ授業料を返してくれるか分からない)、最後の技術ブログはIO流についてだったので、後でJava IO流のメモを添付して、亡くなったC++を記念します(でも、必ずもう一度探して帰ります).
メモ:
IO :★★★★★, 。
: , 。IO , IO 。
:
1: ( ) ( )。
2: , 。
: 。 dvd, , 。 8 , 。 , , 。
? , , GBK unicode , + 。 , , 。 , 。
: : 。
, , , 。 , 。
:InputStream OutputStream
:Reader Writer
, , : , 。
--------------------------------------------------------------------------------------------------------------------
public static void main(String[] args) throws IOException { // 、 IO
/*
1: , 。 , , 。
2: , , , 。
3: , , 。
*/
FileWriter fw = new FileWriter("demo.txt"); // FileNotFoundException
/*
Writer write 。 , ,( )。 ?
*/
fw.write("abcde");
fw.flush(); // , 。
fw.close(); // , java 。 , 。
}
close() flush() :
flush(): , 。
close(): , , 。 。
--------------------------------------------------------------------------------------------------------------------
io :io finally;
FileWriter :
1:window :\r
。 linux:
。
2: , true。
3: :window \\ /
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("abcde");
}
catch (IOException e ){
System.out.println(e.toString()+"....");
}
finally{
if(fw!=null)
try{
fw.close();
}
catch (IOException e){
System.out.println("close:"+e.toString());
}
}
}
--------------------------------------------------------------------------------------------------------------------
FileReader: Reader , 。 -1 , 。
import java.io.*;
class FileReaderDemo {
public static void main(String[] args) throws IOException {
/*
,FileReader 。
*/
FileReader fr = new FileReader("demo.txt");
int ch = 0;
while((ch = fr.read())!= -1) { //
System.out.println((char)ch); // read , 。
}
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
: , 。
import java.io.*;
class FileReaderDemo2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("demo.txt"); // 。
// read(char[]) , 。 , 1024 。
char[] buf = new char[1024];
int len = 0;
while(( len=fr.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
fr.close();
}
}
--------------------------------------------------------------------------------------------------------------------
IO : 。
: 。
: ( ) ;
* 1、 ;
* 2、 ;
* 3、 , , ;
--------------------------------------------------------------------------------------------------------------------
:
Reader: 。 read(char[], int, int) close()。
|---BufferedReader: , , 、 。 , 。 , 。
|---LineNumberReader: 。 setLineNumber(int) getLineNumber(), 。
|---InputStreamReader: : charset 。 , 。
|---FileReader: 。 。 , FileInputStream InputStreamReader。
|---CharArrayReader:
|---StringReader:
-------------------------------------------------
Writer: 。 write(char[], int, int)、flush() close()。
|---BufferedWriter: , , 、 。
|---OutputStreamWriter: : charset 。 , 。
|---FileWriter: 。 。 , FileOutputStream OutputStreamWriter。
|---PrintWriter:
|---CharArrayWriter:
|---StringWriter:
---------------------------------
:
InputStream: 。
|--- FileInputStream: 。 。FileInputStream 。 , FileReader。
|--- FilterInputStream: , , 。
|--- BufferedInputStream: 。
|--- Stream:
|--- ObjectInputStream:
|--- PipedInputStream:
-----------------------------------------------
OutputStream: 。
|--- FileOutputStream: File FileDescriptor 。
|--- FilterOutputStream: 。
|--- BufferedOutputStream: 。
|--- PrintStream:
|--- DataOutputStream:
|--- ObjectOutputStream:
|--- PipedOutputStream:
--------------------------------
, ?
BufferedWriter: , , , 。 。
FileWriter fw = new FileWriter("bufdemo.txt");
BufferedWriter bufw = new BufferedWriter(fw);// 。
for(int x=0; x<4; x++){
bufw.write(x+"abc");
bufw.newLine(); // , 。
bufw.flush();// , 。
}
bufw.close();// , 。
-----------------------------
BufferedReader:
FileReader fr = new FileReader("bufdemo.txt");
BufferedReader bufr = new BufferedReader(fr);
String line = null;
while((line=bufr.readLine())!=null){ //readLine 。
System.out.println(line);
}
bufr.close();
-----------------------------
// , , 。
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));//
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
bufw.write(line.toUpperCase());//
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
------------------------------
: , 。 , N 。 , ? 。
:
1, 。
: , :InputStream、Reader;
: , :OutputStream、Writer;
2, ?
: :Reader
:Writer
: :InputStream
:OutputStream
3, , , ?
。
: (File), ( ), (System.in)
: (File), ( ), (System.out)。
4, ? 。
。
: , , 。
+ 。 , 。
:
InputStreamReader
|--FileReader
OutputStreamWriter
|--FileWrier
, , 。 。
, , , GBK。
FileReader fr = new FileReader("a.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"),"gbk");
,
, FileReader fr = new FileReader("a.txt"); // 。
, 。
= + 。
File = + 。
, , 。
-----------------------------------------------------------------------------------------------
File : 。 。 , 。
File :
1: 。
boolean createNewFile(): , , 。 , , , , 。 。
boolean mkdir(): 。
boolean mkdirs(): 。
2: 。
boolean delete(): 。
void deleteOnExit(): 。
: , , delete 。
window , 。 :java 。 。
3: .
long length(): 。
String getName(): 。
String getPath(): 。
String getAbsolutePath(): 。
String getParent(): , , null。
long lastModified(): 。
File.pathSeparator: ,windows “;”。
File.Separator: ,windows “\”。
4: :
boolean exists(): 。
boolean isDirectory(): 。
boolean isFile(): 。
boolean isHidden(): 。
boolean isAbsolute(): 。
5: 。
boolean renameTo(File dest): 。 + 。
String[] list(): 。 。
list File , list null。 null。 , 。
------------------------------------------------------------------------------------------------
: 。
?
, , 。
: , 。( )。
:
1: 。
2: 。 StackOverflowError 。
。
------------------------------------------------------------------------------------------------
Java.util.Properties: 。Map--Hashtable 。
Map
|--Hashtable
|--Properties: , 。
:1: 。2: 。3: 。
|-- load(): 。
: , , key=value, , = , , , 、 properties 。
|-- store(): , 。
|-- list(): 。
-------------------------------------------------------------------------------------------------
IO : 。
Java.io.outputstream.PrintStream:
1: , 。 。
2: , , , 。
3: .
4: print IOException。
。
PrintStream(File file) : 。
PrintStream(File file, String csn) : 。
PrintStream(OutputStream out) : 。
PrintStream(OutputStream out, boolean autoFlush) : 。
PrintStream(OutputStream out, boolean autoFlush, String encoding) : 。
PrintStream(String fileName) : 。
PrintStream(String fileName, String csn)
PrintStream :1:File 。2: 。3: 。
JDK1.5 。 , 。
, println , printStream true 。 println , 。 print , IO 。
, PrintWriter 。 PrintWrite , , 。
--------------------------------------------------------
PrintWriter: PrintStream , :
:1:File 。2: 。3: 。4: 。
PrintWriter。
。
, 。
// .
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));// :
// : , 。
PrintWriter out = new PrintWriter(new FileWriter("out.txt"),true);// true
String line = null;
while((line=bufr.readLine())!=null){
if("over".equals(line))
break;
out.println(line.toUpperCase());//
}
// :System.in,System.out , jvm 。 。 jvm , 。 , close , 。
out.close();
bufr.close();
------------------------------------------------------------------------------------------------
SequenceInputStream: , 。 。
。 , , , , , 。
, , , 。
, , Vector , 。 ArrayList, ArrayList , 。
? , , , , 。
: 。
: 。
import java.io.*;
import java.util.*;
class SplitFileDemo{
private static final String CFG = ".properties";
private static final String SP = ".part";
public static void main(String[] args) throws IOException{
File file = new File("c:\\0.bmp");
File dir = new File("c:\\partfiles");
meger(dir);
}
// 。
public static void meger(File dir)throws IOException{
if(!(dir.exists() && dir.isDirectory()))
throw new RuntimeException(" , ");
File[] files = dir.listFiles(new SuffixFilter(CFG));
if(files.length==0)
throw new RuntimeException(" .proerpties ");
//
File config = files[0];
// 。
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(config);
prop.load(fis);
String fileName = prop.getProperty("filename");
int partcount = Integer.parseInt(prop.getProperty("partcount"));
//--------------------------
File[] partFiles = dir.listFiles(new SuffixFilter(SP));
if(partFiles.length!=partcount)
throw new RuntimeException(" ");
//---------------------
ArrayList al = new ArrayList();
for(int x=0; x en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
File file = new File(dir,fileName);
FileOutputStream fos = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1){
fos.write(buf,0,len);
}
fos.close();
sis.close();
}
// 。
public static void splitFile(File file)throws IOException{
// 。
FileInputStream fis = new FileInputStream(file);
// 。 。 。
FileOutputStream fos = null;
// 。
File dir = new File("c:\\partfiles");
if(!dir.exists())
dir.mkdir();
// 。
File f = null;
byte[] buf = new byte[1024*1024];
// 。 。
int count = 0;
int len = 0;
while((len=fis.read(buf))!=-1){
f = new File(dir,(count++)+".part");
fos = new FileOutputStream(f);
fos.write(buf,0,len);
fos.close();
}
// , 。 。
// , Properties。
String filename = file.getName();
Properties prop = new Properties();
prop.setProperty("filename",filename);
prop.setProperty("partcount",count+"");
File config = new File(dir,count+".properties");
fos = new FileOutputStream(config);
prop.store(fos,"");
fos.close();
fis.close();
}
}
class SuffixFilter implements FileFilter{
private String suffix;
SuffixFilter(String suffix){
this.suffix = suffix;
}
public boolean accept(File file){
return file.getName().endsWith(suffix);
}
}
-----------------------------------------------------------------------------------------------
RandomAccessFile:
:
1: , 。
2: byte , 。
3: getFilePointer() , seek() 。
4: 。
5: 。
: , 。
class RandomAccessFileDemo{
public static void main(String[] args) throws IOException{
write();
read();
randomWrite();
}
// , 。
public static void randomWrite()throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","rw");
raf.seek(8*4);
System.out.println("pos :"+raf.getFilePointer());
raf.write(" ".getBytes());
raf.writeInt(102);
raf.close();
}
public static void read()throws IOException{
RandomAccessFile raf = new RandomAccessFile("random.txt","r");// 。
// 。
raf.seek(8*1);// 。 : 。
System.out.println("pos1 :"+raf.getFilePointer());
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println(name+"::"+age);
System.out.println("pos2 :"+raf.getFilePointer());
raf.close();
}
public static void write()throws IOException{
//rw: , 。 , 。 。
RandomAccessFile raf = new RandomAccessFile("random.txt","rw");//rw
// , , 。
raf.write(" ".getBytes());
raf.writeInt(97);
raf.close();
}
}
------------------------------------------------------------------------------------------------
: , 。
: , , read, , read , read 。
public static void main(String[] args) throws IOException{
PipedInputStream pipin = new PipedInputStream();
PipedOutputStream pipout = new PipedOutputStream();
pipin.connect(pipout);
new Thread(new Input(pipin)).start();
new Thread(new Output(pipout)).start();
}
------------------------------------------------------------------------------------------------
: : , 。
: , , 。
? transient 。
Serializable: , , , 。 UID。 uid 。 uid, , serialVersionUID id 。 , 。 UID 。
import java.io.*;
class ObjectStreamDemo {
public static void main(String[] args) throws Exception{
writeObj();
readObj();
}
public static void readObj()throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Object obj = ois.readObject();// 。
System.out.println(obj.toString());
}
public static void writeObj()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person("lisi",25)); // 。
oos.close();
}
}
class Person implements Serializable{
private static final long serialVersionUID = 42L;
private transient String name;// transient name
public int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return name+"::"+age;
}
}
-----------------------------------------------------------------------------------------------
DataOutputStream、DataInputStream: 。
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(256);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
System.out.println(num);
dis.close();
-----------------------------------------------------------------------------------------------
ByteArrayInputStream: :
ByteArrayOutputStream: : 。
, , 。
, ? , length 。 , , 。
: ( ) ( ), 。 。
// :
ByteArrayInputStream bis = new ByteArrayInputStream("abcdef".getBytes());
// :
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while((ch=bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());
何か知識点が間違っている場合は、一二を教えてください.