日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

信号转化java_Java基础知识回顾-7

發布時間:2023/12/4 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 信号转化java_Java基础知识回顾-7 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、ByteArrayInputStream、ByteArrayOutputStream

String?str?="ZHANGSAN";

//System.out.println(str.toLowerCase());

ByteArrayInputStream?inputStream?=?newByteArrayInputStream(str.getBytes());

ByteArrayOutputStream?outputStream?=?newByteArrayOutputStream();

intb;

while((b?=?inputStream.read())?!=?-1){

charlowerCase?=?Character.toLowerCase((char)b);

outputStream.write(lowerCase);

}

byte[]?lowerCases?=?outputStream.toByteArray();

System.out.println(newString(lowerCases,0,?lowerCases.length));

全部在內存中完成byte的轉換

2、PrintStream:向目標打印

屬于OutputStream的實現類,向目標(可能是文件、標準輸出屏幕、輸出流、網絡等)打印各種樣式,不過比一般的輸出提供了更多的打印方式,可以打印各種數據類型和樣式等

OutputStream?outputStream?=?System.out;

outputStream.write("helloABC張三".getBytes());

outputStream.close();

列出當前操作系統的系統參數,輸出到文件中

PrintStream?printStream?=newPrintStream("hello.txt");

System.getProperties().list(printStream);

printStream.close();

3、InputStreamReader、OutputStreamWriter, 計算機存儲都是字節流的形式,而讀取到內存中需要識別一個個的字符(人只能識別字符),有可能1個字節就代表一個字符(例如英文),也有可能多個字節才能轉換成一個字符(例如中文),如果中間存在丟失或無法轉換,則看到的就是一堆?

InputStreamReader:將輸入的內容字節變成字符

OutputStreamWriter:將輸出的內容從字符變成字節

4、合并流:SequenceInputStream

File?file1?=newFile("hello.txt");

File?file2?=?newFile("test.txt");

InputStream?inputStream1?=?newFileInputStream(file1);

InputStream?inputStream2?=?newFileInputStream(file2);

SequenceInputStream?sequenceInputStream?=?newSequenceInputStream(inputStream1,

inputStream2);

BufferedInputStream?bufferedInputStream?=?newBufferedInputStream(sequenceInputStream);

intc;

while((c?=?bufferedInputStream.read())?!=?-1){

System.out.print((char)c);

}

bufferedInputStream.close();

sequenceInputStream.close();

inputStream1.close();

inputStream2.close();

測試結果:helloworld

在實驗中,從bufferedInputStream去取到兩個文件大小相加的byte數組中,代碼如下,轉換出來有問題,有點奇怪,只讀到了前一個流中的內容,后面一個流中的內容沒讀取進來。思考中...

File?file1?=newFile("hello.txt");

File?file2?=?newFile("test.txt");

InputStream?inputStream1?=?newFileInputStream(file1);

InputStream?inputStream2?=?newFileInputStream(file2);

SequenceInputStream?sequenceInputStream?=?newSequenceInputStream(inputStream1,

inputStream2);

BufferedInputStream?bufferedInputStream?=?newBufferedInputStream(sequenceInputStream);

intlength?=?(int)?(file1.length()?+?file2.length());

byte[]?b?=newbyte[length];

bufferedInputStream.read(b,?0,?length);

System.out.println(newString(b,0,length));

bufferedInputStream.close();

sequenceInputStream.close();

inputStream1.close();

inputStream2.close();

測試結果如下:hello

5、Zip壓縮與解壓

壓縮程序:

ZipOutputStream?zipOutputStream?=newZipOutputStream(newFileOutputStream("hello.zip"));

zipOutputStream.setLevel(9);

ZipEntry?zipEntry?=?newZipEntry("a.txt");

zipOutputStream.putNextEntry(zipEntry);

BufferedInputStream?bufferedInputStream?=?newBufferedInputStream(newFileInputStream("test.txt"));

intcontent;

while((content?=?bufferedInputStream.read())?!=?-1){

zipOutputStream.write(content);

}

bufferedInputStream.close();

zipOutputStream.closeEntry();

zipOutputStream.flush();

zipOutputStream.close();

解壓程序:

ZipInputStream?zipInputStream?=newZipInputStream(newFileInputStream("hello.zip"));

ZipEntry?zipEntry?=?null;

while((zipEntry?=?zipInputStream.getNextEntry())?!=null)?{

BufferedOutputStream?bufferedOutputStream?=?newBufferedOutputStream(

newFileOutputStream(zipEntry.getName()));

intcontent?=0;

while((content?=?zipInputStream.read())?!=?-1){

bufferedOutputStream.write(content);

}

bufferedOutputStream.flush();

bufferedOutputStream.close();

}

zipInputStream.close();

6、zip壓縮某目錄下的所有文件及子文件

publicvoidzipDirectory(File?pathname,?ZipOutputStream?zipOutputStream)throwsException?{

if(!pathname.isDirectory())?{

return;

}

File[]?files?=?pathname.listFiles();

for(File?file?:?files)?{

if(file.isDirectory())?{

zipDirectory(file,?zipOutputStream);

}?else{

ZipEntry?zipEntry?=?newZipEntry(pathname.getName()?+?File.separator

+?file.getName());

zipOutputStream.putNextEntry(zipEntry);

BufferedInputStream?bufferedInputStream?=?newBufferedInputStream(

newFileInputStream(file));

inti;

while((i?=?bufferedInputStream.read())?!=?-1)?{

zipOutputStream.write(i);

}

bufferedInputStream.close();

zipOutputStream.flush();

zipOutputStream.closeEntry();

}

}

}

問題:中文編碼存在問題,建議選用import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream,由于其存在方法out.setEncoding("gbk");//指定編碼為gbk

6、ThreadLocal

finalThreadLocal?threadLocal?=newThreadLocal();

threadLocal.set("main--");

Thread?thread?=?newThread()?{

@Override

publicvoidrun()?{

threadLocal.set("thread--");

Thread.yield();

System.out.println(Thread.currentThread().getName()?+?":"+?threadLocal.get());

}

};

thread.start();

Thread.yield();

System.out.println(Thread.currentThread().getName()?+?":"+?threadLocal.get());

7、數組和List之間的轉換

數組->List: Arrays.asList(a)

List->數組:list.toArray()

8、正則表達式

(1)^:在[]內表示取反,在外面表示開頭

(2)group

String?regex?="[a-z]{3,5}";

Pattern?pattern?=?Pattern.compile(regex,?Pattern.CASE_INSENSITIVE);

Matcher?matcher?=?pattern.matcher("Abc_defgh_aa_ABCD1");

while(matcher.find())?{

System.out.print("位置[左閉右開區間):"+matcher.start()?+"_"+?matcher.end()?+",?匹配內容:");

System.out.println(matcher.group());

}

測試結果:

位置[左閉右開區間):0_3,?匹配內容:Abc

位置[左閉右開區間):4_9,?匹配內容:defgh

位置[左閉右開區間):13_17,?匹配內容:ABCD

(3)郵件的正則表達式

[\\w[_.]]+@[\\w[_.]]+\\.[\\w]+

(4)"."點號在正則表達式中表示任何字符, 需要表示點號的時候必須轉義\\.

(5)group的分組

分組是以正則表達式中的小括號'()'為標準的,當匹配成功后,group或group(0)表示匹配的整個字符串,group(1)代表正則中第一個小括號匹配到的內容,group(2)代表第二個小括號代表的內容,依次類推

(6)匹配require引入文件

"^[\\s]*#require[\\s]*\\(\"([\\w./]+)\"\\)[\\s]*$"含義為:以任意空白字符開頭,在#require,再任意空白字符,再(",再任意字母、點號、斜線, 再"),最后任意個空白字符結尾

測試代碼:

publicstaticvoidmain(String[]?args)?{

FileInputStream?fileInputStream?=?null;

try{

fileInputStream?=?newFileInputStream(newFile(

"C:/Documents?and?Settings/***/My?Documents/tmp/hello.js"));

}?catch(FileNotFoundException?e)?{

e.printStackTrace();

}

InputStreamReader?inputStreamReader?=?newInputStreamReader(fileInputStream,

Charset.defaultCharset());

BufferedReader?bufferedReader?=?newBufferedReader(inputStreamReader);

String?line?=?"";

try{

while((line?=?bufferedReader.readLine())?!=null)?{

String?requireFile?=?getRequireFile(line);

System.out.println(requireFile);

}

}?catch(IOException?e)?{

e.printStackTrace();

}?finally{

try{

bufferedReader.close();

inputStreamReader.close();

fileInputStream.close();

}?catch(IOException?e)?{

e.printStackTrace();

}

}

}

privatestaticString?getRequireFile(String?line)?{

String?requireFile?=?"";

Pattern?pattern?=?Pattern

.compile("^[\\s]*#require[\\s]*\\(\"([\\w./]+)\"\\)[\\s]*$",?Pattern.MULTILINE);

Matcher?matcher?=?pattern.matcher(line);

while(matcher.find())?{

requireFile?=?matcher.group(1);

}

returnrequireFile;

}

測試文件內容:

var?param;

#require("hello/world_util/alibaba123_utils.js")

#require("/abc/world_util/alibaba12666_utils.js")

測試結果

hello/world_util/alibaba123_utils.js

/abc/world_util/alibaba12666_utils.js

9、FileReader有待完備的地方,只能使用系統默認的字符集,而沒有提供傳遞字符集的構造函數

FileReader繼承了InputStreamReader,但并沒有實現父類中帶字符集參數的構造函數,所以

FileReader只能按系統默認的字符集來解碼

10、阻塞隊列:BlockingQueue

生產者中的 put() 操作會在沒有空間可用時阻塞,而消費者的 take() 操作會在隊列中沒有任何東西時阻塞。

11、信號量:Semaphore, 允許規定數量的線程進入操作,釋放之后其他進入執行

Runnable?limitedCall?=newRunnable()?{

finalRandom????rand??????=newRandom();

finalSemaphore?available?=newSemaphore(3);

intcount?????=0;

publicvoidrun()?{

inttime?=?rand.nextInt(15);

intnum?=?count++;

try{

available.acquire();

System.out.println("Executing?"+"long-running?action?for?"+?time

+?"?seconds...?#"+?num);

Thread.sleep(time?*?1000);

System.out.println("Done?with?#"+?num?+"!");

available.release();

}?catch(InterruptedException?intEx)?{

intEx.printStackTrace();

}

}

};

for(inti?=0;?i?<10;?i++)

newThread(limitedCall).start();

12、死鎖

publicclassDemo06?{

publicstaticvoidmain(String[]?args)?{

DeadLock?deadLock1?=?newDeadLock();

DeadLock?deadLock2?=?newDeadLock();

deadLock1.setFlag(true);

deadLock2.setFlag(false);

newThread(deadLock1).start();

newThread(deadLock2).start();

}

}

classDeadLockimplementsRunnable?{

privatebooleanflag?=false;

publicbooleanisFlag()?{

returnflag;

}

publicvoidsetFlag(booleanflag)?{

this.flag?=?flag;

}

privatestaticObject?object1?=newObject();

privatestaticObject?object2?=newObject();

publicvoidrun()?{

if(flag)?{

synchronized(object1)?{

System.out.println(Thread.currentThread().getName()?+?"?get?object1.");

try{

Thread.sleep(1000);

}?catch(InterruptedException?e)?{

e.printStackTrace();

}

synchronized(object2)?{

System.out.println(Thread.currentThread().getName()?+?"?get?object2.");

}

}

}?else{

synchronized(object2)?{

System.out.println(Thread.currentThread().getName()?+?"?get?object2.");

try{

Thread.sleep(1000);

}?catch(InterruptedException?e)?{

e.printStackTrace();

}

synchronized(object1)?{

System.out.println(Thread.currentThread().getName()?+?"?get?object1.");

}

}

}

}

}

13、反射:通過classloader加載類,標準做法如下:

ClassLoader?cl?=?Thread.currentThread().getContextClassLoader();

if?(cl?==?null)?cl?=?MyClass.class.getClassLoader();?//?fallback

Class?clazz?=?cl.loadClass(name);

14、文件大小限制

錯誤做法:

publicintgetFileSize(File?f)?{

longl?=?f.length();

return(int)?l;

}

正確做法如下:

不支持傳遞超過2GB的文件. 最好的做法是對長度進行檢查, 溢出時拋出異常

publicintgetFileSize(File?f)?{

longl?=?f.length();

if(l?>?Integer.MAX_VALUE)thrownewIllegalStateException("int?overflow");

return(int)?l;

}

15、線程sleep中斷

try{

Thread.sleep(1000);

}?catch(InterruptedException?e)?{

Thread.currentThread().interrupt();

}

or

while(true)?{

if(Thread.currentThread().isInterrupted())break;

}

16、開發中常用術語解釋

java的幾種對象(PO,VO,DAO,BO,POJO)解釋

一、PO:persistant?object?持久對象,可以看成是與數據庫中的表相映射的java對象。最簡單的PO就是對應數據庫中某個表中的一條記錄,多個記錄可以用PO的集合。PO中應該不包含任何對數據庫的操作。

二、VO:value?object值對象。通常用于業務層之間的數據傳遞,和PO一樣也是僅僅包含數據而已。但應是抽象出的業務對象,可以和表對應,也可以不,這根據業務的需要.個人覺得同DTO(數據傳輸對象),在web上傳遞。

三、DAO:data?access?object?數據訪問對象,此對象用于訪問數據庫。通常和PO結合使用,DAO中包含了各種數據庫的操作方法。通過它的方法,結合PO對數據庫進行相關的操作。

四、BO:business?object?業務對象,封裝業務邏輯的java對象,通過調用DAO方法,結合PO,VO進行業務操作。

五、POJO:plain?ordinary?java?object?簡單無規則java對象,我個人覺得它和其他不是一個層面上的東西,VO和PO應該都屬于它。

17、多線售票系統:

classTicketSellerimplementsRunnable?{

privateintticketCount?=10;

@Override

publicvoidrun()?{

while(ticketCount?>0)?{

synchronized(this)?{

if(ticketCount?>0)?{

try{

Thread.sleep(100);

}?catch(InterruptedException?e)?{

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName()

+?"?sell?ticket:?"+?ticketCount--);

}

}

}

}

}

publicclassDemo01?{

publicstaticvoidmain(String[]?args)?{

TicketSeller?ticketSeller?=?newTicketSeller();

newThread(ticketSeller,"Thread?A").start();

newThread(ticketSeller,"Thread?B").start();

newThread(ticketSeller,"Thread?C").start();

newThread(ticketSeller,"Thread?D").start();

}

}

測試結果:

Thread?A?sell?ticket:?10

Thread?A?sell?ticket:?9

Thread?D?sell?ticket:?8

Thread?D?sell?ticket:?7

Thread?D?sell?ticket:?6

Thread?C?sell?ticket:?5

Thread?C?sell?ticket:?4

Thread?C?sell?ticket:?3

Thread?B?sell?ticket:?2

Thread?B?sell?ticket:?1

18、中斷處理

classTicketSellerimplementsRunnable?{

@Override

publicvoidrun()?{

try{

System.out.println("線程啟動");

Thread.sleep(10000);

}?catch(InterruptedException?e)?{

System.out.println("線程被中斷");

//?e.printStackTrace();

}

}

}

publicclassDemo01?{

publicstaticvoidmain(String[]?args)throwsInterruptedException?{

TicketSeller?ticketSeller?=?newTicketSeller();

Thread?thread?=?newThread(ticketSeller,"Thread?A");

thread.start();

System.out.println("====主線程執行===");

Thread.sleep(1000);

//?thread.interrupt();

System.out.println("線程被中斷否:"+?thread.isInterrupted());

thread.interrupt();

System.out.println("線程被中斷否:"+?thread.isInterrupted());

System.out.println("線程被中斷否2:"+?thread.isInterrupted());

System.out.println("主線程是否被中斷:"+?Thread.interrupted());

System.out.println("====主線程結束===");

}

}

測試結果:

====主線程執行===

線程啟動

線程被中斷否:false

線程被中斷否:true

線程被中斷否2:true

主線程是否被中斷:false

線程被中斷

====主線程結束===

結論:

interrupt中斷該線程,isInterrupted檢查該線程是否被中斷,interrupted檢查當前線程是否被中斷。

總結

以上是生活随笔為你收集整理的信号转化java_Java基础知识回顾-7的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。