亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

基于java的IO流的文件讀取系統(tǒng)

chunquedong / 1538人閱讀

摘要:流讀寫基本功能新建文件查看列表寫文件刪除文件查看文件導(dǎo)入的包下面新建一個(gè)類,然后在函數(shù)里初始化一個(gè)方法,方法中用來判斷輸入的值所相對(duì)應(yīng)的功能板塊。這里也只限制于在正常情況下的輸入輸出。

IO流讀寫

基本功能:

新建文件

查看列表

寫文件

刪除文件

查看文件

導(dǎo)入的包:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.plaf.synth.SynthSeparatorUI;

下面新建一個(gè)類,然后在main函數(shù)里初始化一個(gè)方法,方法中用if來判斷輸入的值所相對(duì)應(yīng)的功能板塊。

 public static void main(String[] args) {
            init();//初始化方法

    }
    public static void init(){
        Scanner in = new Scanner(System.in);
        boolean flag = true;
        System.out.println("========小說系統(tǒng)=======");
        while(flag){
            System.out.println("請(qǐng)選擇:1  新建文件    2  查看文件列表    3  寫文件   4  刪除文件   5 查看文件內(nèi)容   6 退出");
            int i = in.nextInt();
            if(i == 1){
                //新建流程
                createFile(in);
            }else if(i == 2){
                //查看文件列表
                showFiles();
            }else if(i == 3){
                //寫文件
                writeFile(in);
            }else if(i == 4){
                //刪除文件
                removeFile(in);
            }else if(i == 5){
                //查看文件內(nèi)容
                showFileContent(in);
            }else if(i == 6){
                //退出
                flag = false;
            }
        }
    }

下面我會(huì)對(duì)每個(gè)板塊的功能進(jìn)行描述:
1.新建功能

//新建文件的方法
    public static void createFile(Scanner in){
            System.out.println("請(qǐng)輸入文件名稱:");
            //給文件命名
            String filename = in.next();
            //這里的文件夾自己創(chuàng)建或者自己定義就好
            File f = new File("e://io/"+filename+".txt");   
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
    }

2.查看文件下的所有文件方法:

//查看文件列表的方法;
    public static void showFiles(){
        File file = new File("E://io");
        //獲取指定文件夾下的文件數(shù)組的特定方法
        File[] files = file.listFiles();
        //遍歷文件夾下的所有文件名
        for(File f:files){
            System.out.println(f.getName());
        }
    }

3.寫文件內(nèi)容的方法

//寫文件方法
        public static void writeFile(Scanner in){
            System.out.println("請(qǐng)輸入要寫的文件的名稱:");
            String filename=in.next();
            System.out.println("請(qǐng)輸入信息:");
            String content=in.next();           
            try {
                BufferedOutputStream out=null;
                out=new BufferedOutputStream(new FileOutputStream("e://io/"+filename+".txt",true));
                out.write(("
	 "+content).getBytes());
                //緩存流需要注意flush方法的使用,flush方法可在緩存區(qū)未滿的情況下,將緩存內(nèi)容寫出到外部設(shè)備中
                out.flush();
                out.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

4.刪除文件

//刪除文件方法
        public static void removeFile(Scanner in){
            System.out.println("請(qǐng)輸入你要?jiǎng)h除的文件名:");
            String filename = in.next();
            File f = new File("e://io/"+filename+".txt");
            if(f.exists()){
                //判斷文件是否存在
                boolean b = f.delete();
                if(b){
                    System.out.println("文件刪除成功");
                }
            }else{
                System.out.println("請(qǐng)重新選擇主菜單");
            }   
        }

5.查看文件內(nèi)容的方法:

//查看文件內(nèi)容的方法
        public static void showFileContent(Scanner in){
        System.out.println("請(qǐng)輸入文件名字:");
        String filename = in.next();
        try {
            BufferedInputStream bufferedInput = null;
            bufferedInput = new BufferedInputStream(new FileInputStream("e://io/"+filename+".txt"));
            int n;
            byte[] buffer = new byte[1024];
            String chunk = null;
            // read():讀入一個(gè)字節(jié),當(dāng)有中文的時(shí)候不可用,所以這里用數(shù)組來存每一個(gè)字符,最后轉(zhuǎn)成字符串輸出
            // 從文件中按字節(jié)讀取內(nèi)容,到文件尾部時(shí)read方法將返回-1
            while ((n = bufferedInput.read(buffer)) != -1) {
                chunk = new String(buffer, 0, n);
                System.out.print(chunk);
            }
            bufferedInput.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }

6.全部源碼

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.plaf.synth.SynthSeparatorUI;


public class System {
    public static void main(String[] args) {
            init();//初始化方法

    }
    public static void init(){
        Scanner in = new Scanner(System.in);
        boolean flag = true;
        System.out.println("========歡迎使用雙體系小說系統(tǒng)=======");
        while(flag){
            System.out.println("請(qǐng)選擇:1  新建文件    2  查看文件列表    3  寫文件   4  刪除文件   5 查看文件內(nèi)容   6 退出");
            int i = in.nextInt();
            if(i == 1){
                //新建流程
                createFile(in);
            }else if(i == 2){
                //查看文件列表
                showFiles();
            }else if(i == 3){
                //寫文件
                writeFile(in);
            }else if(i == 4){
                //刪除文件
                removeFile(in);
            }else if(i == 5){
                //查看文件內(nèi)容
                showFileContent(in);
            }else if(i == 6){
                //退出
                flag = false;
            }
        }
    }
    //新建文件的方法
    public static void createFile(Scanner in){
            System.out.println("請(qǐng)輸入文件名稱:");
            //給文件命名
            String filename = in.next();
            File f = new File("e://io/"+filename+".txt");
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
    }
    //查看文件列表的方法;
    public static void showFiles(){
        File file = new File("E://io");
        File[] files = file.listFiles();//獲取指定文件夾下的文件數(shù)組
        for(File f:files){
            System.out.println(f.getName());
        }
    }
    //寫文件方法
        public static void writeFile(Scanner in){
            System.out.println("請(qǐng)輸入要寫的文件的名稱:");
            String filename=in.next();
            System.out.println("請(qǐng)輸入信息:");
            String content=in.next();           
            try {
                BufferedOutputStream out=null;
                out=new BufferedOutputStream(new FileOutputStream("e://io/"+filename+".txt",true));
                out.write(("
	 "+content).getBytes());
                //緩存流需要注意flush方法的使用,flush方法可在緩存區(qū)未滿的情況下,將緩存內(nèi)容寫出到外部設(shè)備中
                out.flush();
                out.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //刪除文件方法
        public static void removeFile(Scanner in){
            System.out.println("請(qǐng)輸入你要?jiǎng)h除的文件名:");
            String filename = in.next();
            File f = new File("e://io/"+filename+".txt");
            if(f.exists()){
                //判斷文件是否存在
                boolean b = f.delete();
                if(b){
                    System.out.println("文件刪除成功");
                }
            }else{
                System.out.println("請(qǐng)重新選擇主菜單");
            }   
        }
        //查看文件內(nèi)容的方法
        public static void showFileContent(Scanner in){
        System.out.println("請(qǐng)輸入文件名字:");
        String filename = in.next();
        try {
            BufferedInputStream bufferedInput = null;
            bufferedInput = new BufferedInputStream(new FileInputStream("e://io/"+filename+".txt"));
            int n;
            byte[] buffer = new byte[1024];
            String chunk = null;
            // read():讀入一個(gè)字節(jié)
            // 從文件中按字節(jié)讀取內(nèi)容,到文件尾部時(shí)read方法將返回-1
            while ((n = bufferedInput.read(buffer)) != -1) {
                chunk = new String(buffer, 0, n);
                System.out.print(chunk);
            }
            bufferedInput.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
}

注:當(dāng)然啦,只是實(shí)現(xiàn)了基本的功能,對(duì)于一些細(xì)節(jié)沒有處理,比如輸入的時(shí)候如果有字符怎么處理等情況。這里也只限制于在正常情況下的輸入輸出。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/71243.html

相關(guān)文章

  • 從設(shè)計(jì)者角度理解Java IO

    摘要:通過多個(gè)裝飾類實(shí)現(xiàn)責(zé)任鏈模式,它將對(duì)一個(gè)輸入流的不同處理分散到不同的中去。 1、基本概念 1.1、InputStream 最基本的字節(jié)輸入流,抽象類,定義了讀取原始字節(jié)的所有基本方法1.1.1、public abstract int read() throws IOException 讀取一個(gè)字節(jié)的方法,最基礎(chǔ)的方法1.1.2、public int read(byte b[], in...

    Flink_China 評(píng)論0 收藏0
  • 第十五章 輸入輸出系統(tǒng)

    摘要:在包下主要包括輸入輸出兩種流,每種輸入輸出流又可分為字節(jié)流和字符流兩大類。輸入輸出是從程序運(yùn)行所在的內(nèi)存的角度而言的。的輸入流主要由和作為基類,而輸出流主要由和作為基類。 本章主要參考和摘自瘋狂java講義上面的(java編程思想的后面看過后有新的內(nèi)容再補(bǔ)充進(jìn)去吧)?! ≥斎胼敵鍪撬谐绦蚨急匦璧牟糠帧褂幂斎霗C(jī)制允許程序讀取外部數(shù)據(jù)(包括磁盤、光盤等存儲(chǔ)設(shè)備上的數(shù)據(jù)和用戶輸入的...

    hankkin 評(píng)論0 收藏0
  • 高薪程序員&面試題精講系列22之說說JavaIO流,常用哪些IO流?

    摘要:一面試題及剖析今日面試題今天壹哥帶各位復(fù)習(xí)一塊可能會(huì)令初學(xué)者比較頭疼的內(nèi)容,起碼當(dāng)時(shí)讓我很有些頭疼的內(nèi)容,那就是流。在這里壹哥會(huì)從兩部分展開介紹流,即與流。除此之外盡量使用字節(jié)流。關(guān)閉此輸入流并釋放與流相關(guān)聯(lián)的任何系統(tǒng)資源。 一. 面試題及剖析 1. 今日面試題 今天 壹哥 帶各位復(fù)習(xí)一塊可...

    fnngj 評(píng)論0 收藏0
  • JavaNIO

    摘要:也支持鎖定部分內(nèi)容,使用即可,其中為時(shí),表明該鎖是一個(gè)共享鎖,可以允許多個(gè)縣城讀取文件,但阻止其他進(jìn)程獲得該文件的排他鎖。當(dāng)為時(shí),表明是一個(gè)排他鎖,它將鎖住對(duì)該文件的讀寫。默認(rèn)獲取的是排他鎖。 Java的NIO BufferedReader有一個(gè)特征,就是讀取輸入流中的數(shù)據(jù)時(shí),如果沒有讀到有效數(shù)據(jù),程序?qū)⒃诖颂幾枞摼€程的執(zhí)行(使用InputStream的read方法從流中讀取數(shù)據(jù)時(shí),...

    mrcode 評(píng)論0 收藏0
  • java學(xué)習(xí)(九) —— javaFile文件操作及IO流概述

    摘要:字節(jié)流可以處理所有以為單位存儲(chǔ)的文件,也就是說可以處理所有的文件,但是在處理字符的速度上不如字符流。文件字節(jié)輸入流的讀取時(shí),是直接同字節(jié)流中讀取的。原理就是在字節(jié)流的基礎(chǔ)上增加了編解碼的操作。 前言 流是干什么的:為了永久性的保存數(shù)據(jù)。 IO流用來處理設(shè)備之間的數(shù)據(jù)傳輸(上傳和下載文件) java對(duì)數(shù)據(jù)的操作是通過流的方式。 java用于操作流的對(duì)象都在IO包中。 java IO系統(tǒng)...

    ralap 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<