Java Learning (Five)

Java Learning (Five)

Tags (separated by spaces): Java


final usage

final is not commonly used in java, but it provides the ability to define constants in c language, such as us, not only that, final also allows you to control your members, or whether the method is a class that can be overridden or inherited and other features, these features make the final has an indispensable role in java, it is necessary to know and master when one of the keywords is also learning java.

   final members: When you define variables in the class, precede it with the final keyword, that is to say, this variable will not be changed once initialized, where the meaning of the immutable basic types whose value is immutable , and for which the object reference variable is no longer changed. When it can be initialized in two places, one at its definition, it means that the definition of the final variable to assign it directly, and second in the constructor, note that these two places can only choose one, either in the definition a value, or a value in the constructor, not both at the same time to a value in the definition, and to another value in the constructor. Another use is defined method parameters for the final, the basic types of variables to do so and there is no practical significance, because the basic types of variables are passed by value when calling the method, which means that you can change in the method the variable parameters without affecting the call statement. However, for the object variable, but it is very practical, because when passing the object variable is passed in its reference, so that you modify an object variable in the process will affect the object variable call statement, when you do not need to change in the method as the object variable parameters declared explicitly use the final, you will prevent inadvertent changes affect the calling method.

   final Methods: The method is declared as final, it shows that you already know this method provides the functionality you already meet the requirements, do not need to be expanded, and will not allow any class that inherits from this class to override this method, but you can still inherit inherit this method, that can be used directly. There is another mechanism called inline, it will make you when you call the final method, a method of directly inserted into the body at the call, rather than a routine method calls, such as saving a breakpoint, push, etc., which may make your program efficiency has improved, but when you approach the body is very large, or in many places you call this method, your call will be rapid expansion of the main body of code, but may affect the efficiency, so you have to careful final defined method.

   final categories: the final time when you used to like him, you will need to think carefully, because a final class can not be inherited by anyone, that means in such an inheritance tree is a leaf class, and this class design has been considered perfect without the need to modify or extend. For the final class member, you can define it as final, may not be final. As for the method, since the final of their class relationship, naturally became the final type. You can also clear the way to the final class plus a final, but apparently does not make sense.

Use the File class JavaI / O portion of

In java, I / O (input / output) is a relatively tedious, because see significant running effect, but the input / output part of all programs is necessary - using an input mechanism that allows the program to read the external data (including data from a magnetic disk, an optical disk storage device), a user input data; using the output mechanism that allows program recording operation state, the output of the program data to the magnetic disk, optical disk storage devices.

 The Java I / O classes and interfaces supported by the lower java.io package, at java.io package includes an input, an output two kinds of IO streams, each input and output streams can be divided into a byte stream and character stream two categories. Wherein the byte stream bytes to process the input, output operations, and places the character processed character stream input, output operations.

The Java IO streams using a decorative design pattern, IO will flow into the upper and bottom node process flow stream, wherein the stream is directly associated with the physical node for the storage nodes and the bottom - of different physical nodes of the node acquired streamed there may be some differences, but the program may be in different physical nodes into a uniform flow of process flow package, thereby allowing the use of a unified program input. Code to read the output resources of different physical storage nodes.

By following a major portion of this class record knowledge, specific code as follows:

import java.io.File;
import java.util.Date;
 
 
 
/**
 * 
 * @author Android将军
 *
 */
/*
 * File类是java.io包下代表与平台无关的文件和目录,也就是说,如果希望在程序中操作文件和目录,都可以通过File
 *类来完成。值得指出的是,不管是文件还是目录都是使用File来操作的,File能新建、删除、重命名文件和目录,File
 *不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入/输出流。
 *  一旦创建了File对象后,就可以调用File对象的方法来访问,File类提供了很多方法来操作文件和目录。
 */
public class FileStudy {
    
 
    private static File file;
    /**
     * 传入指定目录的文件或目录,获取该对象的详细信息
     * @param uri 传入的文件路径或文件目录
     */
    public static void createSysout(String uri)
    {
        file=new File(uri);
        if(file.isFile())//判断File对象所对应的是否是文件,而不是目录
        {
            //返回此File对象所表示的文件名
            System.out.println("文件名:"+file.getName());
            //返回此File对象所对应的路径名
            System.out.println("文件路径:"+file.getPath());
            //返回此File对象所对应的绝对路径所对应的File对象
            System.out.println("该文件所对应的绝对路径所对的File的名字:"+file.getAbsoluteFile().getName());
            //返回此File对象所对应的绝对路径名。
            System.out.println(file.getAbsolutePath());
            //返回此File对象所对应目录(最后一级子目录)的父目录名.
            System.out.println(file.getParent());
            
            //重命名此File对象所对应的文件或目录,如果重命名成功,则返回true,否则返回false
//          System.out.println(file.renameTo(new File("TestNewName.txt")));//重命名之后,该file所对应的文件就没有了。
            //判断File对象所对应的文件或目录是否存在
            System.out.println("文件是否存在:"+file.exists());
            //判断File对象所对应的文件和目录是否可写
            System.out.println("文件是否可写:"+file.canWrite());
            //判断File对象所对应的文件和目录是否可读
            System.out.println("文件是否可读:"+file.canRead());
            //判断File对象所对应的是否是目录,而不是文件
            System.out.println("该对象是否为目录:"+file.isDirectory());
            //判断File对象所对应的文件或目录是否是绝对路径。该方法消除了不同平台的差异,可以直接判断File对象是否为绝对路径。
            //在Unix/Linux/BSD等系统上,如果路径名开头是一条斜线(/),则表明该File对象对应一个绝对路径;在Windows等系统上,如果路径开头
            //是盘符,则说明它是一个绝对路径。
            System.out.println("是否为绝对路径:"+file.isAbsolute());
            System.out.println("文件的最后修改时间:"+new Date(file.lastModified()).toString());
            System.out.println("文件内容的长度:"+file.length());
            
        }
        
    }
}

http://blog.csdn.net/android_jiangjun/article/details/41683915

Guess you like

Origin www.cnblogs.com/hit-zb/p/11129778.html