1. 程式人生 > 實用技巧 >php 讀取檔案,批量構造sql語句寫入資料庫

php 讀取檔案,批量構造sql語句寫入資料庫

1,I/O java的輸入輸出系統,解決不同源端和接收端之間,多種不同形式的通訊,(順序,隨機存取,快取,二進位制,按字元,按位元組,按行)

解決方案:建立不同類,裝飾者模式

1.1,磁碟操作--檔案管理:File類

  • 可以代表某個檔案,也可以表示一個目錄下一組檔案
  • 只表示檔案和目錄資訊,但不表示檔案內容

①:方法:

  • File(String path) path如果是路徑,表示的為目錄。若是檔名,表示檔案
  • File(String path,String name) path路徑下的name檔案
  • String getName() 獲得檔名
  • String getPath() 獲得檔案路徑
  • String getParent() 獲得上一級目錄
  • boolean isFile() 當前檔案是否是檔案
  • boolean isDirectory() 當前檔案是否是資料夾
  • File[] listFiles() 返回當前目錄(是資料夾)下的檔案和目錄
  • File[] listFiles(FileFilter filter) 返回當前目錄下滿足指定過濾器的檔案和目錄

②示例:遞迴列車一個目錄下所有檔案

    static int deep=0;
    public static void listAllFiles(File dir){
        if(dir==null||!dir.exists())
            return;
        for(int i=0;i<deep;i++)
            System.out.print("   ");
        if(dir.isFile()){
            System.out.println(dir.getName());
            return;
        }
        System.out.println(dir.getName());
        deep++;
        for(File file:dir.listFiles()){
            listAllFiles(file);
        }
        deep--;
    }

呼叫:

File file=new File("D:\\-_-\\春招\\1");
listAllFiles(file);

輸出:

③示例:檔案過濾:

過濾器:Filter

自動義檔名過濾器:

static class Filter implements FilenameFilter{
String extend;
Filter(String extend){
this.extend=extend;
}
@Override
public boolean accept(File dir, String name) {
return name.endsWith("."+extend);
}
}
搜尋方法:
public static void search(String filter){
File dir=new File("D:\\-_-\\春招\\1");
Filter filter1=new Filter(filter);
System.out.println(filter+"檔案:");
File files[]=dir.listFiles(filter1);
for(File f:files){
if(f.isFile()){
System.out.println("檔名:"+f.getName());
}
}
}

呼叫結果:

search("pdf");

1.2,位元組流

①InputStream和OutputStream,--抽象類

  • InputStream主要方法:

int read() 讀取一個位元組,0-255;若達到流末尾沒有位元組,返回-1

int read(byte[] b) 讀取多個位元組放到b中,返回讀取位元組數,流末尾返回-1

void close() 流操作結束,必須關閉

  • OutputStream主要方法:

void write(int b) int32位4位元組,將b的低8位寫入輸出流,高24位被忽略

void write(byte[] b) 將b.length個位元組寫入輸出流。

void flush() 刷空輸出流,

void close() 流操作完畢後必須關閉。

②InputStream子類:

從檔案輸入,從Byte陣列輸入,管道

  • FileInputSream:檔案輸入流。它通常用於對檔案進行讀取操作
  • ByteArrayInputStream: 字元陣列輸入流,從Byte[]中以位元組為單位讀取,
  • PipedInputStream:管道位元組輸入流,它和PipedOutputStream一起使用,能實現多執行緒間的管道通訊
  • ObjectOutputStream:用於序列化
  • FielterInputStream: 裝飾者模式中處於裝飾者,具體的裝飾者都要繼承它,所以在該類的子類下都是用來裝飾別的流的,也就是處理類。
  • BufferedInputStream: 快取流,對處理流進行裝飾增強,內部有一個緩衝區,用來存放位元組,快取區存滿後發出,不是一個位元組或兩個

位元組這樣發。效率高

  • DataInputStream: 資料輸入流,用來裝飾其他流,允許我們讀取不同型別的基本資料型別以及String物件(readByte(),readFloat()等等。)

③OutputStream子類:

  • FileOutputstream
  • ByteArrayOutputStream
  • PipedOutputStream
  • ObjectOutputStream
  • FielterOutputStream
  • BufferedOutoutStream
  • DataOutputStream
  • PrintStream: Systerm.in Systerm.out

④裝飾者模式

  • 為物件動態新增功能
  • 裝飾者(如FilterInputStream)與具體元件(如FileIputStream---BufferedInputStream)都繼承至元件(如InputStream)。

具體元件的方法實現不需要依賴於其他物件,而裝飾者組合了一個元件,這樣可以裝飾其他元件。

所謂裝飾即擴充套件被裝飾者的功能。

  • 例如例項化一個具有快取功能的位元組流物件,只需要在FileInputStream物件再套一層BufferedStream物件即可

FileInputStream fileInputStream = new FileInputStream(filePath);

BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

⑤示例:實現檔案複製:

public static void copyFile(String src,String dir)throws IOException{
FileInputStream in=new FileInputStream(src);
FileOutputStream out=new FileOutputStream(dir);

byte[] buffer=new byte[2*1024];
int cnt;
while ((cnt=in.read(buffer,0,buffer.length))!=-1){
out.write(buffer,0,cnt);
}
in.close();
out.close();
}

呼叫結果:

String src=".\\test.txt";
String dir="test2.txt";
copyFile(src,dir);

⑥示例:使用位元組快取流:

  • BufferedIputStream(InputStream in) 通過一個底層輸入流in物件建立緩衝流物件,緩衝區大小是預設的,預設值8192
  • BufferedOutputStream(OutputStream out)通過一個底層輸出流 out 物件建立緩衝流物件,緩衝區大小是預設的,預設值 8192。
public static void bufferCopyFie(String src,String dir)throws IOException{
FileInputStream in=new FileInputStream(src);
FileOutputStream out=new FileOutputStream(dir);
BufferedInputStream bin=new BufferedInputStream(in);
BufferedOutputStream bout=new BufferedOutputStream(out);

long startTime=System.nanoTime();
byte[] buffer=new byte[1024];
int len=bin.read(buffer);

while (len!=-1){
bout.write(buffer,0,len);
len=bin.read(buffer);
}
long time=System.nanoTime()-startTime;
System.out.println("緩衝器耗時: "+(time/1000000.0)+"毫秒");
}
public static void copyFile(String src,String dir)throws IOException{
FileInputStream in=new FileInputStream(src);
FileOutputStream out=new FileOutputStream(dir);

long startTime=System.nanoTime();
byte[] buffer=new byte[1024];
int cnt;
while ((cnt=in.read(buffer,0,buffer.length))!=-1){
out.write(buffer,0,cnt);
}
in.close();
out.close();
long time=System.nanoTime()-startTime;
System.out.println("無緩衝器耗時: "+(time/1000000.0)+"毫秒");
}

執行結果:

String src=".\\test.txt";
String dir="test2.txt";
copyFile(src,dir);
bufferCopyFie(src,dir);

有緩衝區效率高

1.3,字元流

  • 為了國際化,java採用Unicode字元,為雙位元組字元。

①字元編碼:

編碼:編碼是從一種形式或格式轉換為另一種形式的過程也稱為計算機程式語言的程式碼簡稱編碼

計算機中儲存資訊的最小單位是一個位元組,即8個bit.

  • ASCII碼:共有128個,用一個位元組的低7位表示。
  • ISO8859-1: 在ASCII碼的基礎上涵蓋了大多數西歐的語言字元,仍是單位元組編碼,總共表示256個。
  • GB2321:全稱為《資訊交換用漢字編碼字符集基本集》雙位元組編碼,
  • GBK:《數字交換用漢字編碼字符集》,單,雙,四位元組編碼,與GB2312編碼相容。擴充套件了繁體字等
  • UTF-16:具體定義了Unicode字元在計算機中的存取方法,採用2位元組表示Unicode轉換格式。定長編碼
  • UTF-8:採用一種變長技術,不同字元可以1-6位元組。

②String編碼:

String s="一二三";

byte[] bytes=s.getBytes("UTF-8");

String s1=new String(bytes,"UTF-8");

③字元流與位元組流的區別

  • 位元組流沒有緩衝區,是直接輸出的,而字元流是輸出到緩衝區的。因此在輸出時,位元組流不呼叫colse()方法時,

資訊已經輸出了,而字元流只有在呼叫close()方法關閉緩衝區時,資訊才輸出。要想字元流在未關閉時輸出資訊,

則需要手動呼叫flush()方法。

  • 讀寫單位不同,位元組流以位元組(8bit)為單位,字元流以字元為單位。
  • 處理物件不同,位元組流是處理所有型別資料(如圖片,視訊等),而字元流只能處理字元型別的資料。

只處理純文字資料,優先考慮字元流,除此之外使用位元組流。

④字元流與位元組流轉換

  • InputStreamReader(InputStream in), 將位元組流in轉換位字元流,預設編碼
  • InputStreamReader(InputStream in,String charsetName) ,將位元組流in轉換位字元流,charsetName是指定字符集

ASCII,ISO-8859-1,UTF-8,UTF-16等。如果字符集不支援會丟擲UnsupportedEncodingException異常。

  • OutputStreamWriter(OutputStream out) 將位元組流out轉換位字元流,預設字符集
  • OutputStreamWrite(OutputStream out,String charsetName) 將位元組流out轉換為字元流物件,charsetName是指定字符集

如果字符集不支援會丟擲UnsupportedEncodingExseption.

⑤Reader和Writer---抽象類

Reader主要方法:

  • int read() 讀取一個字元,0-65535(0x00-oxffff)。達到檔案末尾返回-1.
  • int read(char[] c) 將字元讀入char陣列中,返回實際讀取字元數。末尾返回-1.
  • void close() 流操作結束後必須關閉。

Writer主要方法:

  • void write(int c) 將int c寫入輸出流,int4位元組,寫入低16位,高16位忽略
  • void write(char[] c) 將字元陣列c寫入輸出流
  • void write(char[] c,int off,int len) 把字元陣列c從下標off開始的len個字元寫入輸出流
  • void write(String s) 將s中的字元寫入輸出流
  • void write(String s,int off,int len)把字串s從下標off開始的len個字元寫入輸出流
  • void flush() 刷空輸出流,,並輸出所有被快取的字元。
  • void cloae() 流操作結束必須關閉

Reader子類

  • CharAraryReader:
  • StringReader:
  • PipedReader:
  • BufferedRead:並不是Filter子類。
  • InputStreamRead:
  • FilterRead:

Writer子類:

  • CharArrayWriter
  • StringWriter
  • PipedWriter
  • BufferedWriter
  • OutStreamWriter
  • PriterWriter
  • FilterWriter

⑥示例:從檔案逐行輸出內容

public static void readFile(String path)throws IOException{
FileReader fileReader=new FileReader(path);
BufferedReader bfileReader=new BufferedReader(fileReader);

String line;
while((line=bfileReader.readLine())!=null){
System.out.println(line);
}
bfileReader.close();
}

⑦示例:檔案複製:

public static void copyFileReader(String src,String dir)throws IOException{
FileReader in=new FileReader(src);
FileWriter out=new FileWriter(dir);
char[] buffer=new char[10];

int len=in.read(buffer);
while(len!=-1){
String str=new String(buffer);
out.write(buffer,0,len);
len=in.read(buffer);
}
}

使用緩衝:

readLine()方法丟掉 換行符

寫完呼叫float()方法才會將緩衝區資訊寫入

public static void bufferCopyFileReader(String src,String dir){
try{

FileReader in=new FileReader(src);
FileWriter out=new FileWriter(dir);
BufferedReader bin=new BufferedReader(in);
BufferedWriter bout=new BufferedWriter(out);


long startTime=System.nanoTime();
String line=bin.readLine();
while (line!=null){
bout.write(line);
bout.newLine();----readLine()方法丟掉換行符,這裡補上;
//System.out.println(line);
line=bin.readLine();

}
bout.flush();----flush!!!!!
long time=System.nanoTime()-startTime;
System.out.println("緩衝器耗時: "+(time/1000000.0)+"毫秒");
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}

1.4,物件操作

序列化:序列化就是將一個物件轉換成位元組序列,方便儲存和運輸。

儲存物件資訊,---程式停止執行物件就消失了。

實現輕量級永續性,意味著物件的生命週期不取決於程式是否執行。

  • 序列化:ObjectOutputStream.writeObject()
  • 反序列化:ObjectInputStream.readObject()

參考:

Java-IO流

技術面試必備基礎知識 CyC2018.pdf

java程式設計思想

java從小白到大牛精簡版

電子書(pdf)獲取途徑:史上最全的 Java 學習資料,PDF 電子書大合集