Io file path Problem __java Foundation

Source: Internet
Author: User
Tags parent directory stub


Package Com.yh.io;


Import Java.io.File;
Import java.io.IOException;




/*
* Absolute path: The physical path from the letter to the beginning
* Relative path: The path relative to a specified location
* Java project in eclipse: Default path with relative path relative path: current project path
*
*
*/


public class Test01filepath {


public static void Main (string[] args) {
TODO auto-generated Method Stub

Absolute path
File File=new file ("D:\\software\\teststring\\a.txt");

Relative path
File File2=new file ("B.txt");
try {
File.createnewfile ();
File2.createnewfile ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


}


}













Package Com.yh.io;


Import Java.io.File;


/**
* Advanced methods of the File class
* string[] List () returns an array of strings that specify the files and directories in the directory represented by this path name
* file[] Listfiles () returns an abstract path array group that represents the file in the directory represented by this abstract path name
*
*/
public class Test03file {


public static void Main (string[] args) {
TODO auto-generated Method Stub
Current project file
File File=new file ("d:\\software\\teststring");

System.out.println ("File path:" +file.getabsolutefile ());

String[] Str=file.list ();

for (int i=0;i<str.length;i++) {
System.out.println ("list ():" +str[i]);
}

File[] Files=file.listfiles ();

for (File f:files) {
System.out.println ("Listfiles" +f);
}
System.out.println ("--------Traverse file----------");
Iterate through all the files under file
Listfiles (file);
}
List all files under File


public static void Listfiles (file file) {


File[] Files=file.listfiles ();

for (int i=0;i<files.length;i++) {
Determine if the file is a directory
if (Files[i].isdirectory ()) {
Recursion
Listfiles (Files[i]);
}else{
System.out.println ("File:" +files[i].getname ());
}
}


}


}





File Object


Package Com.yh.io;

Import Java.io.File;
Import java.io.IOException;


/*
* Absolute path: The physical path from the letter to the beginning
* Relative path: The path relative to a specified location
* Java project in eclipse: Default path with relative path relative path: current project path
*
*
*/

public class Test01filepath {

public static void Main (string[] args) {
TODO auto-generated Method Stub

Absolute path
File File=new file ("D:\\software\\teststring\\a.txt");

Relative path
File File2=new file ("B.txt");
try {
File.createnewfile ();
File2.createnewfile ();
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

}


Package Com.yh.io;

Import Java.io.File;
Import java.io.IOException;

/**
*file Instantiation: File (String pathname) creates a new File instance by converting the given pathname string to an abstract pathname.
*boolean CreateNewFile () Creates a new, empty file if and only if the file does not exist.
*boolean mkdir () creates the directory specified by this abstract path name.
Boolean mkdirs () creates the directory specified by this abstract path name, including all parent directories that are required but not present.
public Boolean exists () to determine whether a file exists
public boolean isdirectory () to determine whether a given path is a directory
public boolean Delete () Delete file if it does not exist, the return value is False, no error

Properties of File:
The public long Length () method returns the size of the file
String GetName () returns the name of the file or directory represented by this abstract path name.
String GetParent () returns the path name string for the parent of this abstract path name, or null if the pathname does not specify a parent directory.
The File Getparentfile () returns the abstract pathname of the parent directory of this abstract pathname, or null if the pathname does not specify a parent directory.

*/

public class Test02file {

public static void Main (string[] args) {
TODO auto-generated Method Stub

File File1=new file ("1.txt");

File File2=new file ("Test1/2.txt");

File Dir1=new file ("Test1");

File Dir2=new file ("Test1/test2");

File Dir3=new file ("Test3/test4");


File File4=new file ("Test3/test4/3.txt");

try {
if (!file1.exists ()) {
CreateNewFile () Create a file
System.out.println ("File1 Create:" +file1.createnewfile ());
}

if (!dir1.exists ()) {
mkdir () Single Directory
System.out.println ("Dir1 Create:" +dir1.mkdir ());
}


System.out.println ("File2 Create:" +file2.createnewfile ());

System.out.println ("Dir2 Create:" +dir2.mkdir ());

Mkdirs () Create a multi-tiered directory
System.out.println ("Dir3 Create:" +dir3.mkdirs ());

System.out.println ("File4 Create:" +file4.mkdir ());

Isdirectory () to determine whether a file is a directory
System.out.println ("File2 is directory:" +file2.isdirectory ());

System.out.println ("Dir3 is Direcotry:" +dir3.isdirectory ());


System.out.println ("Delete file1:" +file1.delete ());

System.out.println ("file4 Delete:" +file4.delete ());

System.out.println ("File2 Length:" +file2.length ());
System.out.println ("File2 Name:" +file2.getname ());

System.out.println ("File2 parent ():" +file2.getparent ());
System.out.println ("File2 parentfile ():" +file2.getparentfile ());

System.out.println ("File4 Parent ():" +file4.getparent ());
System.out.println ("File4 parentfile ():" +file4.getparentfile ());

System.out.println ("File1 parent ():" +file1.getparent ());


catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

}

Package Com.yh.io;

Import Java.io.File;
Import Java.io.FileInputStream;
/*
* Input stream: InputStream abstract class
* Materialized: New FileInputStream ()
* int read () reads a byte of data from this input stream.
int read (byte[] b) reads up to b.length bytes of data from this input stream into a byte array.

*/
Import java.io.FileNotFoundException;

public class Test04inputstream {

public static void Main (string[] args) {
TODO auto-generated Method Stub

Create a file
File File=new file ("TEST1/2_RENAME.DC");


try {
Create an input stream object
FileInputStream ins=new fileinputstream (file);

*///read one character at a time
int a=0;
while ((A=ins.read ())!=-1) {
System.out.println ((char) a);
}*/

Read bytes of the specified length

Byte[] Buf=new byte[1024];

int len=-1;
while ((Len=ins.read (BUF))!=-1) {
System.out.println (New String (Buf,0,len));
}


catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}

}

Package Com.yh.io;

Import Java.io.File;

/**
* Advanced methods of the File class
* string[] List () returns an array of strings that specify the files and directories in the directory represented by this path name
* file[] Listfiles () returns an abstract path array group that represents the file in the directory represented by this abstract path name
*
*/
public class Test03file {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Current project file
File File=new file ("d:\\software\\teststring");

System.out.println ("File path:" +file.getabsolutefile ());

String[] Str=file.list ();

for (int i=0;i<str.length;i++) {
System.out.println ("list ():" +str[i]);
}

File[] Files=file.listfiles ();

for (File f:files) {
System.out.println ("Listfiles" +f);
}
System.out.println ("--------Traverse file----------");
Iterate through all the files under file
Listfiles (file);
}
List all files under File

public static void Listfiles (file file) {

File[] Files=file.listfiles ();

for (int i=0;i<files.length;i++) {
Determine if the file is a directory
if (Files[i].isdirectory ()) {
Recursion
Listfiles (Files[i]);
}else{
System.out.println ("File:" +files[i].getname ());
}
}


}

}

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.