溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

java _io_文件讀取標(biāo)準(zhǔn)步驟

發(fā)布時間:2020-07-11 14:04:09 來源:網(wǎng)絡(luò) 閱讀:223 作者:wx5d21d5e6e5ab1 欄目:編程語言

File f =new File("~"); //創(chuàng)建源
InputStream is =new FileInputStream(f); //選擇流
is.read() 讀取單個數(shù)據(jù),并使游標(biāo)下移 //操作(讀取)
is.close() //釋放資源,輸入流讀取后必須釋放資源

public class test{

public static void main(String[]args) 
{
    //創(chuàng)建源
    File f=new File("C:/Users/10853/eclipse-workspace/hell/src/hell/abc");
    InputStream is =null;//提升is的作用域,避免在try中聲明后,作用域
                        //只在try,finally中語句無法執(zhí)行
    //選擇流
    try {
         is =new FileInputStream(f);
    //操作(讀取)    
        int temp;
        while((temp=is.read())!=-1) //temp=is.read()表達(dá)式整體的值就是temp的值
        {                           //is.read()會讀取單個數(shù)據(jù),當(dāng)數(shù)據(jù)讀取完畢時,返回-1
            System.out.println((char)temp);
        }

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally
    {
        try {
            if(null!=is)//當(dāng)is創(chuàng)建成功時才執(zhí)行關(guān)閉
            {
                is.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

} 
}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI