• 路径常量,绝对路径与相对路径,构造File对象——高淇JAVA300讲笔记之IO和File


    案例一:路径分隔符,名称分隔符

     1 package com.bjsxt.io.file;
     2 
     3 import java.io.File;
     4 
     5 /**
     6  * 两个常量
     7  * 1、路径分隔符 ;
     8  * 2、名称分隔符 (windows)   /(Linux等)
     9  *
    10  */
    11 public class Demo01 {
    12     public static void main(String[] args) {
    13         System.out.println(File.pathSeparator);  // ;
    14         System.out.println(File.separator);  // 
    15         //路径表示形式
    16         String path = "E:\xp\test\2.jpg";
    17         path = "E:"+File.separator+"xp"+File.separator+"test"+File.separator+"2.jpg";
    18         
    19     }
    20     
    21 }

    案例二:绝对路径与相对路径构造File对象

     1 package com.bjsxt.io.file;
     2 
     3 import java.io.File;
     4 
     5 /**
     6  * 相对路径与绝对路径构造File对象
     7  * 1、相对路径
     8  * 2、绝对路径
     9  * 
    10  *
    11  */
    12 public class Demo02 {
    13     public static void main(String[] args) {
    14         String parentPath = "E:/xp/test";
    15         String name = "2.jpg";
    16         //相对路径
    17         File src = new File(parentPath,name);
    18         src = new File(new File(parentPath),name);
    19         //输出
    20         System.out.println(src.getName());  //2.jpg
    21         System.out.println(src.getPath());  //E:xp	est2.jpg
    22         
    23         //绝对路径
    24         src = new File("E:/xp/test/2.jpg");
    25         //输出
    26         System.out.println(src.getName());  //2.jpg
    27         System.out.println(src.getPath());  //E:xp	est2.jpg
    28         
    29         //没有盘符:以user.dir构建
    30         src = new File("test.txt");
    31         System.out.println(src.getName());  //test.txt
    32         System.out.println(src.getPath());  //test.txt
    33         System.out.println(src.getAbsolutePath());  //E:gaoqi-workspaceIO	est.txt
    34     }
    35 }
  • 相关阅读:
    cocos2d-x 屏幕适配
    C# 做一个指定概率的抽奖程序
    Cocos2d-x 开发小记(二):控件
    Cocos2d-x 开发小记(一):基本动作
    Cocos2d-x v2.2.2版本+Win7+VS2010环境搭建
    使用C#从XML中批量删除指定节点
    使用NSIS脚本制作一个安装包
    C++解析命令行参数(仿C语言args)
    关于 Source Engine 2007 网络通信的分析
    关于OpenGL游戏全屏模式的设置
  • 原文地址:https://www.cnblogs.com/swimminglover/p/8408932.html
Copyright © 2020-2023  润新知