Simple analysis of "file" and "stream" in Java

Source: Internet
Author: User
Tags rar

Simple analysis of file and stream in Java file class Simple file Common method

Create a File object, verify that the files exist, create them if they do not exist, and then demonstrate this part of the file's class, such as the name, size, and so on

Create a File object, verify that the files exist, create them if they do not exist, and then Filepackage wfu;import java.io.file;import java.io.ioexception;import Java.util.date;import Java.util.scanner;public class Test1 {public static void main (string[] args) {System.out        . println ("Please input the file ' s name");        Scanner Scanner = new Scanner (system.in);        Enter file path name from console String pathName = Scanner.next (); Creates a Filea object based on the name of the path file File = new file (pathName);//From here it can be seen that the parameter in this party is a string//If the file does not exist, create an IF        (!file.exists ())            {try {file.createnewfile ();            } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();        }} System.out.println ("File exists:" +file.exists ());        System.out.println ("is the file:" +file.isfile ());        System.out.println ("is the directory:" + file.isdirectory ());        System.out.println ("name" + File.getname ());        SYSTEM.OUT.PRINTLN ("path" + File.getpath ()); SySTEM.OUT.PRINTLN ("absolute path:" + File.getabsolutepath ());        System.out.println ("Last modified:" + new Date (File.lastmodified ()). ToString ());    SYSTEM.OUT.PRINTLN ("File size:" + file.length ()); }}

Run results

f:yjlblog.exe文件是否存在:true是文件吗:true是目录吗:false名称yjlblog.exe路径f:yjlblog.exe绝对路径:f:\\yjlblog.exe最后的修改时间:Sat Nov 25 19:48:42 CST 2017文件大小:0
Description

lastModifued()The last modification time for a method in the preceding code to return a file. The time is a long integer that is the number of milliseconds between the point in time (January 1, 1970, 00:00:00 GMT). Therefore Date encapsulation is done through the L class, which is the new Date object, where the parameter gets the number of milliseconds.

File list Device
    • String[] List (): Returns all files and subdirectories in this directory when the file class object is a directory
    • File[] List (): Returns an array of all file objects under the path corresponding to the file object
      For both of these methods, the second approach is a little better, why is that, because she can call file a lot of ways
Give me a chestnut OAO.

Defines a class that demonstrates using the list () method to f enumerate the names of directories or files under the root directory.

public class Test1{    public static void main(String[] args) {        //根据路径名称穿件File 对象        File file = new File("f:");        //得到文件名列表        if (file.isDirectory())        {            String[] fileNames = file.list();            //利用for-each 打印各个文件名称            for (String fileNamesSeeall : fileNames)            {                System.out.println(fileNamesSeeall);            }        }    }}

Run results

$RECYCLE.BIN111360RecAdobe After Effects CC 2014精简绿色版Adobe After Effects CC 2014精简绿色版.rarAdobe Premiere Pro Preview Filesamp皮肤hlw.mp4java 大项目实时更新JAVA项目MLO4.2.1.rarmmexport1510996082011.pngmysql-connector-java-5.1.20-bin.jarnavicat_permium_x64navicat_permium_x64.7zorcle 安装遇到的问题.txtSystem Volume InformationTeamViewer Corporate 12.0.88438.7ztest2.txttypecho-theme-apollo-1.0.2.zipWPS+Office+2016+WPS+Office+2016+.7zyjlblog.exeyjlblog.txt个人作业图书数据管理系统-MySQL (V2.0).zip壁纸备份娱乐宿舍小软件屏幕录像专家 v20170328.exe工作室帮助文档憋笑.prproj我的码农之路收藏数据库启用新建文件夹杂七杂八泡泡表情王老师文件班级信息电子图书素材纲要实践课安排.doc网站苹果apple107秒快闪苹果apple107秒快闪.rar表情包课件笔记赵彬富过错不应犯之,功绩亦不应漠之.docx重要资料音乐

Description

From the implementation of the results can be seen, the list() method will be f out of the disk (all let you see the whining ~ ~), but do you have to indicate which is the document which is the directory it qaq.

Give the second chestnut (~ ̄▽ ̄) ~*

Then put the above implementation of the function is not realized, is the directory is the file pull out yo!!!!

package wfu;import java.io.File;public class Test1{    public static void main(String[] args) {        //根据路径名称创建File 对象        File file = new File("f:");        //得到文件名列表        if (file.isDirectory())        {            File[] files = file.listFiles();            //利用foreach 获取每个File对象            for (File f:files)            {                if (f.isFile()){                    System.out.println("文件: "+ f);                }                else{                    System.out.println("目录: " + f);                }            }        }    }}

Run results

Catalog: F:\ $RECYCLE. Bin directory: f:\111 directory: F:\360Rec directory: F:\Adobe After Effects cc 2014 Lite Green file: F:\Adobe After Effects cc 2014 Lite green version. rar directory: F:\amp skin file : F:\hlw.mp4 directory: F:\java Theme catalog: F:\java big Project Live Update Catalog: F:\JAVA project file: F:\MLO4.2.1.rar file: F:\mmexport1510996082011.png file: f:\ Mysql-connector-java-5.1.20-bin.jar Directory: f:\navicat_permium_x64 file: f:\navicat_permium_x64.7z file: f:\orcle Installation encountered a problem with the. txt directory: F:\System Volume information file: F:\TeamViewer Corporate 12.0.88438.7z File: F:\test2.txt file: f:\ Typecho-theme-apollo-1.0.2.zip Directory: f:\wps+office+2016+ file: f:\wps+office+2016+.7z file: F:\yjlblog.exe file: f:\ Yjlblog.txt directory: F:\ personal directory: f:\ job Files: F:\ book Data management system-mysql (V2.0). zip directory: f:\ wallpaper directory: f:\ backup directory: F:\ Entertainment directory: f:\ Dorm directory: f:\ small software files: F:\ Screen Recorder expert V20170328.exe directory: f:\ Studio directory: f:\ Help documentation file: f:\. prproj directory: f:\ My yard path directory: F:\ Favorites directory: f:\ database enable directory: f:\ new folder directory: F:\ Assorted directory: f:\ Bubble Expression Directory: F:\ Mr. Wang file directory: f:\ class Information Catalog: F:\ e-book catalog: f:\ Material Files: f:\ Outline Practice class arrangement. Doc directory: f:\ site Directory: f:\ Apple apple107 PST Flash file: f:\ Apple apple107 seconds flash. rar directory: F:\ expression package Directory: F:\ Courseware Note directory: F:\ Zhao bin rich file: F:\ fault should not be committed, merit also should not be desert. docx Directory: f:\ Important information directory: F:\ SoundLe 
Flow
    • Input stream (Inputsream): Data can only be read from and not written to
    • Output stream (OutputStream): Data can only be written to, not read from
    • BYTE stream: The base unit of a stream that is processed in bytes (8 bits of bit)
    • Character streams: The stream in which the base unit is processed in a stream is a character (16-bit Unicode)
    • Node Flow: Not written, too long, the book has ...
    • Filter flow: ......

To be continued .....

Simple analysis of "file" and "stream" in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.