久久99精品久久久久久琪琪,久久人人爽人人爽人人片亞洲,熟妇人妻无码中文字幕,亚洲精品无码久久久久久久

關(guān)于java文件流的簡單示例

時間:2019-05-15 13:10:07下載本文作者:會員上傳
簡介:寫寫幫文庫小編為你整理了多篇相關(guān)的《關(guān)于java文件流的簡單示例》,但愿對你工作學習有幫助,當然你在寫寫幫文庫還可以找到更多《關(guān)于java文件流的簡單示例》。

第一篇:關(guān)于java文件流的簡單示例

package myjava;

import java.io.*;

public class InputStreamDemo1 {

//根據(jù)文件返回一個輸入流 private FileInputStream getFileInputStream(File file){FileInputStream fis=null;try{fis=new FileInputStream(file);}catch(IOException e){System.out.println(e.getMessage());}return fis;} //根據(jù)文件返回一個輸輸出流 private FileOutputStream getFileOutputStream(File file){FileOutputStream fos=null;try{fos=new FileOutputStream(file);}catch(IOException e){System.out.println(e.getMessage());}catch(Exception e1){System.out.println(e1.getMessage());}return fos;} public static void main(String args[]){File input=new File(“G:Java網(wǎng)絡編程.pdf”);File output=new File(“G:output.txt”);InputStreamDemo1 isd=new InputStreamDemo1();FileInputStream fis=isd.getFileInputStream(input);FileOutputStream fos=isd.getFileOutputStream(output);int b=0;

}} try {while((b=fis.read())!=-1){fos.write(b);} fis.close();fos.close();} catch(Exception e){System.out.println(e.getMessage());}

第二篇:java 文件與輸入輸出流

實驗2文件與輸入輸出流(1)

一、實驗目的? 能夠使用File類表示文件或目錄,獲取相關(guān)信息,并進行文件操作; ? 能夠利用InputStream和OutputStream的子類進行字節(jié)讀、寫操作,明白其優(yōu)點及不足;

? 能夠用FileInputStream和FileOutputStream進行文件讀寫的操作; ? 理解“逐層包裝”思想,能夠利用“數(shù)據(jù)流”(DataInputStream和DataOutputStream)包裝字節(jié)流,方便各類數(shù)據(jù)的讀寫;

? 能夠利用“緩沖字節(jié)流”(BufferedInputStream和BufferedOutputStream)包裝字節(jié)流,加快數(shù)據(jù)的讀寫速度;

? 熟知System.in和System.out是PrintStream的實例。

二、實驗步驟

在Eclipse環(huán)境中導入項目“code(lab_2)”,然后按要求完成各小題:

1.打開FileTest.java文件,請按下列要求進行操作:

(1)按要求填充程序所缺代碼;

(2)程序的運行需要用到一個命令行參數(shù),請分別用一個文件、目錄作參數(shù)來運行程序,看一看結(jié)果有什么不同。

(在Eclipse中設置命令行參數(shù)方法:Run/Open Run Dialog ?/(x)=Arguments設置)

2.在上一題的基礎上,修改程序,使之具備輸出指定目錄下所有子目錄中文件絕對路徑名、大小的功能,如下所示:

子目錄:C:jdk1.6.0sample

子目錄:C:jdk1.6.0samplewebservices

子目錄:C:jdk1.6.0samplewebservicesEbayServer

文件: C:jdk1.6.0samplewebservicesEbayServerbuild.properties,大小: 512 字節(jié) 文件: C:jdk1.6.0samplewebservicesEbayServerbuild.xml,大小: 3168 字節(jié)1

……

提示:參考課件 FileSpace.java內(nèi)容,通過一個以“路徑名”為參數(shù)的靜態(tài)方法來實現(xiàn):該方法先判斷“路徑名”是一個文件,還是一個目錄?如果是文件,則輸出其絕對路徑和大??;若為一個目錄,則先顯示目錄絕對路徑,再列出該目錄下的所有子目錄和文件,通過循環(huán)和遞歸方法來執(zhí)行后續(xù)處理。

3.文件FileOutputStreamTest.java的功能是:利用FileOutputStream類向myfile.txt文件寫入'0'~'9'和“用字節(jié)流寫入文件內(nèi)容”,請?zhí)畛涑绦蛩贝a,并運行程序。然后打開myfile.txt文件,查看其內(nèi)容是否與要求相符?

4.文件FileInputStreamTest1.java的功能是:利用FileInputStream類以“逐字節(jié)”方式讀取myfile.txt文件內(nèi)容,并輸出。請?zhí)畛涑绦蛩贝a,并運行程序。問題:為什么程序輸出的內(nèi)容為亂碼?

5.在FileInputStreamTest1.java的基礎上,編寫程序FileInputStreamTest2.java,利用FileInputStream類以“字節(jié)數(shù)組”方式讀取myfile.txt文件內(nèi)容,能正確輸出,解決亂碼問題。

思考題:亂碼問題是怎樣解決的?

6.若要將信息“Java開發(fā)典型模塊大全”(書名)、“明日科技”(作者)、79.5(價格)等信息以UTF、double類型保存到文件books.txt中,請用“數(shù)據(jù)流”類編程實現(xiàn)。

第三篇:Java 實驗 文件管理與IO流

作業(yè)要求:每個題保存為一個.java文件,保存在同一工程文件夾中,文件夾的名字為E:Java你的班級+姓名,例如:E:Java信息11張三。

注意:上交的程序包含程序的執(zhí)行結(jié)果,以注釋的形式附在程序后面。

實驗六文件管理與I/O流

一、實驗目的1.熟悉用文件File類創(chuàng)建、刪除、查看文件或目錄。

2.字節(jié)流、字符流、緩沖流、隨機流等流式文件的創(chuàng)建,讀寫操作。

3.用字符流和緩沖流從鍵盤接受字符串的方法。

二、實驗內(nèi)容

1.先運行該程序。源文件是sy6_1.java。然后按【思考問題】分析程序。

import java.io.*;

public class sy6_1{

public static void main(String[] args)throws Exception

{

int x=0;

File Mypath;

Mypath=new File(“E:aaaa”);

if(!Mypath.exists())

{System.out.println(“創(chuàng)建新目錄”);Mypath.mkdir();}

else System.out.println(“目錄已存在”);

File Myfile1=new File(Mypath,“myfile1.txt”);

File Myfile2=new File(Mypath,“myfile2.txt”);

File Myfile3=new File(Mypath,“myfile3.txt”);

FileInputStream Fin=new FileInputStream(Myfile1);

FileOutputStream Fout=new FileOutputStream(Myfile1);

DataOutputStream Dout=new DataOutputStream(new FileOutputStream(Myfile2));

DataInputStream Din=new DataInputStream(new FileInputStream(Myfile2));

PrintWriter PWout=new PrintWriter(new FileWriter(Myfile3));

RandomAccessFile RAread=new RandomAccessFile(Myfile3,“r”);

String str;

int num1;

BufferedReader buf;//緩沖流

buf=new BufferedReader(new InputStreamReader(System.in));

System.out.print(“請輸入一個小于255整型數(shù):”);

while(!(str=buf.readLine()).equalsIgnoreCase(“q”))

{ System.out.print(“請輸入另一個小于255整型數(shù),按Q結(jié)束:”);

num1=Integer.parseInt(str);

Fout.write(num1);

}Fout.close();

System.out.println(“你剛輸入的數(shù)據(jù)是:”);

while((x=Fin.read())!=-1)

{ System.out.println(x);

}Fin.close();

System.out.print(“請輸入int范圍內(nèi)整型數(shù):”);

while(!(str=buf.readLine()).equalsIgnoreCase(“q”))

{ System.out.print(“請輸入另一個整型數(shù),按Q結(jié)束:”);

num1=Integer.parseInt(str);

Dout.writeInt(num1);

}Dout.close();

int leng=Din.available()/4;

int xxx=0;

while(xxx

{ xxx++;

x=Din.readInt();

System.out.println(x);

} Din.close();

System.out.print(“請輸入第一個字符串:”);

while((str=buf.readLine())!=null)

{ System.out.print(“請輸入另一個字符串,按Ctrl+Z結(jié)束:”);

PWout.println(str);//寫入myfile3.txt中

} PWout.close();

RAread.seek(0);

while(RAread.getFilePointer()

{System.out.println(RAread.readLine());//從myfile3.txt中一行一行讀并輸出在控制臺上

} RAread.close();

System.out.println(“完成”);

}

}

【思考問題】

① 本程序共用到哪幾種流式文件?都用于做什么?

答:基本輸入輸出流:System.in.輸入(從鍵盤)

System.out.輸出(顯示器)

字節(jié)流類:FileOutputStream 文件輸出

FileInputStream 文件輸入

DataOutputStream數(shù)據(jù)輸出

DataInputStream數(shù)據(jù)輸入

字符流類:PrintWriter輸入

緩沖文件流:BufferedReader

② 運行完程序后,請用“我的電腦”找到創(chuàng)建的文件,并分別打開文件看其內(nèi)容,你

看到的是你輸入的數(shù)據(jù)嗎?

答:myfile1和myfile2中的數(shù)據(jù)非輸入數(shù)據(jù),myfile3中可以看見輸入的數(shù)據(jù)。③ 將創(chuàng)建輸入流對象Fin放在輸出流Fout前,看發(fā)生什么?

④ 對第二種流式文件判斷文件占用字節(jié)的長度用available()方法,而此處用int

leng=Din.available()/4;為什么除以4?

2.按照第1題的內(nèi)容,修改程序要求每次重新運行不覆蓋原內(nèi)容,把所有其它流式文件全部改用隨機流式文件來實現(xiàn),新的數(shù)據(jù)填加在文件尾,然后讀出校驗。

import java.io.*;

public class sy6_2 {

public static void main(String[] args)throws Exception {

File Mypath;

Mypath = new File(“E:aaa”);

if(!Mypath.exists())

{System.out.println(“創(chuàng)建新目錄”);Mypath.mkdir();}

else System.out.println(“目錄已存在”);

File Myfile1 = new File(Mypath, “myfile1.txt”);

File Myfile2 = new File(Mypath, “myfile2.txt”);

File Myfile3 = new File(Mypath, “myfile3.txt”);

RandomAccessFile rf1 = new RandomAccessFile(Myfile1, “rw”);

RandomAccessFile rf2 = new RandomAccessFile(Myfile2, “rw”);

RandomAccessFile rf3 = new RandomAccessFile(Myfile3, “rw”);

String str;

int num1;

BufferedReader buf;//緩沖流

buf = new BufferedReader(new InputStreamReader(System.in));

System.out.print(“請輸入一個小于255整型數(shù):”);

rf1.seek(rf1.length());//指針移到文件尾進行寫操作

while(!(str=buf.readLine()).equalsIgnoreCase(“q”))

{ System.out.print(“請輸入另一個小于255整型數(shù),按Q結(jié)束:”);num1=Integer.parseInt(str);

rf1.write(num1);//將整型數(shù)作為ascii碼值所對應的字符寫入myfile1.txt中}

rf1.seek(0);//指針移到文件頭進行讀操作

int x=0;

while((x=rf1.read())!=-1)

{

System.out.println(x);

} rf1.close();

System.out.print(“請輸入int范圍內(nèi)整型數(shù):”);

rf2.seek(rf2.length());

while(!(str = buf.readLine()).equalsIgnoreCase(“q”)){

System.out.print(“請輸入另一個整型數(shù),按Q結(jié)束:”);

num1 = Integer.parseInt(str);

rf2.writeInt(num1);

}

int x1 = 0;

for(int l = 0;l

{

rf2.seek(l*4);

x1 = rf2.readInt();

System.out.println(x1);

}

rf2.close();

System.out.print(“請輸入第一個字符串:”);

rf3.seek(rf3.length());

while((str = buf.readLine())!= null){

System.out.println(“請輸入另一個字符串,按Ctrl+Z結(jié)束:”);rf3.writeUTF(str);//寫入myfile3.txt中

}

rf3.seek(0);

while(rf3.getFilePointer()< rf3.length()){

System.out.println(rf3.readUTF());//從myfile3.txt中讀出字符串并輸出在控制臺上

}

rf3.close();

System.out.println(“完成”);

}

}

三、實驗報告要求

1.回答第1題【思考問題】提出的問題。

2.寫出第二題要求的源程序。

第四篇:使用java輸入輸出流實現(xiàn)文件的復制

使用java輸入輸出流實現(xiàn)文件的復制:

public class Test {

public static void copy(File source,File target){

InputStream in=null;

OutputStream out=null;

try {

in=new BufferedInputStream(new FileInputStream(source));out=new BufferedOutputStream(new FileOutputStream(target));byte[] buff=new byte[1024];

while(in.read(buff)>0){

out.write(buff);

}

} catch(Exception e){

// TODO: handle exception

e.printStackTrace();

}finally{

try {

if(in!=null)

in.close();

if(out!=null)

out.close();

} catch(Exception e2){

// TODO: handle exception

}

}

}

public static void main(String[] args){

// TODO Auto-generated method stub

}

}

第五篇:java流實驗內(nèi)容及代碼

實驗7 流(2學時)

一、實驗目的1.熟悉流類庫中各種常用流的使用方法。

2.能夠使用流類實現(xiàn)基本的文件讀寫。

二、實驗內(nèi)容

1.編寫程序,在控制臺窗口提示輸入兩個整數(shù),然后接收這兩個整數(shù),并輸出它們的和。(要求:鍵盤輸入通過流封裝System.in獲取,不要使用Scanner類)

import java.io.*;System.out.println(x);System.out.println(“y”);public class Num1 {n=in.readLine();public static void main(String []args)y=Integer.parseInt(n);{System.out.println(y);int x=0,y=0;}catch(IOException e)BufferedReader in=new BufferedReader({

newSystem.out.println(“error”);InputStreamReader(System.in));}

String n;int s=x+y;

try{System.out.println(s);System.out.println(“x”);}

n=in.readLine();}

x=Integer.parseInt(n);

2.設計學生類Student,屬性:編號(整型);姓名(字符串),成績(整型)。編寫一個程序:要求:(1)輸入3個學生的姓名和成績,將其姓名和成績保存到data.txt中;(2)然后從該文件中讀取數(shù)據(jù),求得這三個學生的平均成績。

import java.io.*;DataOutputStream dout = newDataOutputStream(public class num2 {newpublic static void main(String[]args)FileOutputStream(“D:data.txt”));{String n;

BufferedReader in=new BufferedReader(for(int i=0;i<5;i++)new{

InputStreamReader(System.in));n=in.readLine();

int num=Integer.parseInt(n);try{String name=in.readLine();

n=in.readLine();int grade=Integer.parseInt(n);dout.writeBytes(num+“rn”);dout.writeBytes(name+“rn”);dout.writeBytes(grade+“rn”);}dout.close();}catch(IOException e1){int num=Integer.parseInt(n);

n=din.readLine();String name=n;n=din.readLine();int grade=Integer.parseInt(n);ave+=grade;}System.out.println(“平均成績”+ave*1.0/5);System.out.println(“文件寫入失敗”);}try{DataInputStream din =new DataInputStream(new FileInputStream(“D:data.txt”));int ave=0;String n;for(int i=0;i<5;i++){n=din.readLine();

三、實驗要求

完成程序設計并提交實驗報告。

在“);失敗”);} }

}catch(FileNotFoundException e){System.out.println(“文件不存}catch(IOException e2){System.out.println(”文件讀取}

下載關(guān)于java文件流的簡單示例word格式文檔
下載關(guān)于java文件流的簡單示例.doc
將本文檔下載到自己電腦,方便修改和收藏,請勿使用迅雷等下載。
點此處下載文檔

文檔為doc格式


聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻自行上傳,本網(wǎng)站不擁有所有權(quán),未作人工編輯處理,也不承擔相關(guān)法律責任。如果您發(fā)現(xiàn)有涉嫌版權(quán)的內(nèi)容,歡迎發(fā)送郵件至:645879355@qq.com 進行舉報,并提供相關(guān)證據(jù),工作人員會在5個工作日內(nèi)聯(lián)系你,一經(jīng)查實,本站將立刻刪除涉嫌侵權(quán)內(nèi)容。

相關(guān)范文推薦

    Java中輸出流(五篇材料)

    從輸入流中當前位置讀入一個字節(jié)的二進制數(shù)據(jù),以此數(shù)據(jù)為低位字節(jié),補足16位的整型量(0~255)后返回,若輸入流中當前位置沒有數(shù)據(jù),則返回-1 int read(byte b) 在數(shù)組中,并返回所讀......

    課題4:流和文件

    實習課題21:流和文件 實訓目的 ? 會使用java.io包中的常用類進行操作 實訓內(nèi)容 練習一: 在C:下創(chuàng)建一個名為Letter的目錄。 編寫一個程序來顯示下列選項: 1:編寫請假條 2:查看請假......

    JAVA課程設計之簡單聊天室

    課程設計報告 課程名稱 課程代碼 題 目 年級/專業(yè)/班 學生姓名 學 號 指導老師 開題時間 完成時間 Java程序設計 簡單聊天室 孟青 09308910203 I : : : : : : : : : 目 錄......

    實驗12:Java高級IO流程序設計

    實驗12:Java高級I/O流程序設計 實驗時間:實驗地點: 一、實驗目的及要求 (1)掌握文件類File的使用。 (2)理解隨機存取文件類RandomAccessFile的使用。 二、實驗設備環(huán)境及要求三、實......

    Java語言實驗指導書-實驗10 流范文

    一、實驗目的 熟悉文件流操作方法。 應用文件流操作文件。 二、實驗內(nèi)容 利用流操作,在屏幕上顯示文本文件內(nèi)容及文件的路徑、修改時間、大小、長度、 可讀性和可修......

    Java5_3_文件流_字符流_FileReader、FileWriter

    /* * 字符流的輸入與輸出 */ package com.tt.IO; import java.io.*; public class Demo6 {public static void main(String args) {// TODO Auto-generated method stub//......

    JAVA(IO流方法大全)(優(yōu)秀范文五篇)

    package Stream; import java.io.*; public class Io { public void test1 throws Exception{ File file=new File("E:/txt.txt"); if(file.exists){System.out.println......

    Java IO流將信息寫入文本文件檔

    public class Test{ public static void main(String[] args) { try { BufferedWriter bw = new BufferedWriter(new FileWriter("data.txt")); //文件將會創(chuàng)建在程序所在的......

主站蜘蛛池模板: 欧美日韩综合一区二区三区| 亚洲精品久久久久久久久久吃药| 丰满迷人的少妇特级毛片| 成年女人18级毛片毛片免费| 亚洲欧美中文日韩v在线观看| 丰满少妇被粗大猛烈进人高清| 久久夜色精品国产噜噜亚洲sv| 久久久久久免费毛片精品| 国产免费拔擦拔擦8x高清在线| 最新国产亚洲人成无码网站| 无码人妻丰满熟妇啪啪网不卡| 性色欲网站人妻丰满中文久久不卡| 国产毛片精品av一区二区| 九九综合va免费看| 亚洲国产成av人天堂无码| 婷婷五月综合缴情在线视频| 色播亚洲视频在线观看| 性欧美丰满熟妇xxxx性久久久| 亚洲色欲在线播放一区二区三区| 国产欧美日韩a片免费软件| 亚洲色精品vr一区二区三区| 免费播放一区二区三区| 中文幕无线码中文字蜜桃| 国产成人精品综合久久久久| 内射人妻视频国内| 无码国产偷倩在线播放| 狠狠热精品免费视频| 欧美黑人又大又粗xxxxx| 国产一区二区在线影院| 成人国产亚洲精品a区| 亚洲av无码成人网站在线观看| 我把护士日出水了视频90分钟| 久久亚洲国产五月综合网| 免费观看全黄做爰大片国产| 东京热人妻无码人av| 亚洲 小说区 图片区 都市| 人人妻人人澡人人爽超污| 国内精品久久久久影院薰衣草| 北条麻妃无码| 亚洲欧美另类久久久精品| 午夜131美女爱做视频|