您好,欢迎来到尚车旅游网。
搜索
您的当前位置:首页File类

File类

来源:尚车旅游网
1.编写java应用程序,使用FileInputStream类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。 import java.io.*; public class Class1 { public static void main (String[] args) { try { //FileInputStream fis=new FileInputStream(\"c:\\\\windows\\\\Config.txt\"); FileInputStream fis=new FileInputStream(\"Class1.java\"); int n; while((n=fis.read())!=-1) System.out.print((char)n); fis.close(); } catch(IOException e) { System.out.println(e.toString()); }}}

2. 编写java应用程序,使用FileInputStream类对象读取程序本身(或其他目录下的文件)到字节数组中,并显示在屏幕上(或存储为其他文件)。 import java.io.*;//读取程序本身,显示在屏幕上 public class Class1 { public static void main (String[] args) { try { FileInputStream fis=new FileInputStream(\"Class1.java\"); byte[] b=new byte[fis.available()]; System.out.println(\"文件流的大小:\"+fis.available()); int n=fis.read(b); myprint(b); System.out.print(\"实际读取的字节数:\"+n); fis.close(); } catch(IOException e) {System.out.println(e.toString());} } static void myprint(byte[] x) { for(int i=0;i3.编写应用程序,程序中创建一个文件输入流对象fis,读取当前目录下文本文件test1.txt,该文件内容有如下两行文本:

Java program is easy. I like it.

从文件输入流fis中读取5个字节数据存放到数组b中,字节数据存放的位置从数组下标3开始。将读取的数据在屏幕输出。 import java.io.*; public class Class1 { public static void main( String[ ] args ) throws IOException { File file = new File(\"./test1.txt\"); FileInputStream fis = new FileInputStream( file); int n=0; byte b[]=new byte[8]; n=fis.read(b,3,5); fis.close(); for(int i=3;i5.在程序所在的目录下有子目录b,目录b下有文本文件testb.txt。编写应用程序,创建文件对象:

File file=new File(\"b/testb.txt\");

通过文件对象file得到它的文件名、相对路径、绝对路径、父目录名。 import java.io.*; public class Class1

{ }

public static void main( String args[ ] ) { }

File file=new File(\"b/testb.txt\");

System.out.println(\"文件名:\" + file.getName( ) ); System.out.println(\"相对路径:\" + file.getPath( ) );

System.out.println(\"绝对路径:\" + file.getAbsolutePath( ) ); System.out.println(\"父目录名:\" + file.getParent( ) );

6. 请编写一个名为Class1.java的Application,其功能为:测验文件Class1.java是否存在并

输出其长度。 import java.io.*; public class Class1 { public static void main (String[] args) { File f1=new File(\"./Class1.java\"); System.out.println(\"is exist:\"+f1.exists( )); System.out.println(\"file length:\"+f1.length( )); } }

7. 编写应用程序:创建目录c:\emp,并创建一个文件2.txt,向该文件中写入字符串\"The first

snow came.\"共5次。 import java.io.*; public class Class1 { public static void main(String args[]) throws IOException { File file1=new File(\"c:\\\emp\"); file1.mkdir(); File file2=new File(file1,\"2.txt\"); FileOutputStream out1=new FileOutputStream(file2); String s=\"The first snow came.\"; byte[] b=s.getBytes(); for(int i=0;i<5;i++) { out1.write(b,0,b.length); } }

}

8. 编写java应用程序,使用FileReader类对象读取程序本身(或其他目录下的文件)并显示在屏幕上。

import java.io.*;//读取程序本身,显示在屏幕上 public class Class1 { public static void main (String[] args) { try { FileReader fis=new FileReader(\"Class1.java\"); //FileReader fis=new FileReader(\"c:\\\\windows\\\\Config.txt\"); int n; while((n=fis.read())!=-1) System.out.print((char)n); fis.close(); } catch(IOException e) { System.out.println(e.toString()); } }

}

9. 编写应用程序,创建BufferedReader的对象,从某个文本文件中的字符输入数据流中读取一

行字符(该文件与程序在同一目录下),跳过10个字节后将其显示出来。 import java.io.*; public class Class1 { public static void main(String args[]) throws IOException { BufferedReader b_reader=new BufferedReader(new FileReader(\"1.js\")); String str; b_reader.skip(10); str=b_reader.readLine(); System.out.println(str); } }

10. 编写程序,完成文件复制功能,即将a.txt文件内容复制到b.txt文件中去。 答案如下:

import java.io.*; /**

*描述:将a.txt文件内容复制到b.txt文件中去。

*/

public class copyfile { public static void main(String[] args) { String oldPath=\"c:\\\est\\\\a.txt\"; String newPath=\"c:\\\est\\\\b.txt\"; try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); //判断文件存是否存在--存在 if (oldfile.exists()) { //读入a文件 InputStream inStream = new FileInputStream(oldPath); //读入b文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { //字节数 文件大小 bytesum += byteread; //输出到控制台 System.out.println(bytesum); //写入文件 fs.write(buffer, 0, byteread); } //关闭文件流 inStream.close(); } } catch (Exception e) { System.out.println(\"复制单个文件操作出错\"); e.printStackTrace(); } } }

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务