Example usage for java.io File getName

List of usage examples for java.io File getName

Introduction

In this page you can find the example usage for java.io File getName.

Prototype

public String getName() 

Source Link

Document

Returns the name of the file or directory denoted by this abstract pathname.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    // create new files
    File f = new File("c:/");

    System.out.print(f.getName());

}

From source file:FileFilterDemo.java

public static void main(String[] args) {
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
        public boolean accept(File f) {
            return f.getName().toLowerCase().endsWith(".gif") || f.isDirectory();
        }//w ww . ja  v a  2 s .c  o  m

        public String getDescription() {
            return "GIF Images";
        }
    });

    int r = chooser.showOpenDialog(new JFrame());
    if (r == JFileChooser.APPROVE_OPTION) {
        String name = chooser.getSelectedFile().getName();
        System.out.println(name);
    }
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C:/File/Demo.txt");

    System.out.println(file.getName());
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File("C:/Demo.txt");

    System.out.println("File name is : " + file.getName());
}

From source file:GetURL.java

public static void main(String args[]) throws Exception {
    URL url = new URL("http://www.google.com");
    InputStream urlstream = url.openStream();
    byte[] buffer = new byte[0];
    byte[] chunk = new byte[4096];
    int count;// w  ww .j a  v a  2  s .c o  m

    while ((count = urlstream.read(chunk)) >= 0) {
        byte[] t = new byte[buffer.length + count];
        System.arraycopy(buffer, 0, t, 0, buffer.length);
        System.arraycopy(chunk, 0, t, buffer.length, count);
        buffer = t;
    }

    String filename = (url.getFile()).replace('/', File.separatorChar);
    File f1 = new File(filename);
    filename = f1.getName();
    FileOutputStream f = null;
    f = new FileOutputStream(filename);
    f.write(buffer);
    f.close();
}

From source file:de.ipbhalle.metfrag.mainTools.CountFiles.java

public static void main(String[] args) {

    String pathToScan = "/home/swolf/MOPAC/100spec_SDF_GC-EIMS/sdf_calculated/";
    String[] ext = { "cml" };

    Collection<File> filesRecursively = (Collection<File>) FileUtils.listFiles(new File(pathToScan), ext, true);
    int count = 0;
    for (File file : filesRecursively) {
        if (file.getName().contains("_Combined"))
            count++;//from  w ww .  j a  v a2s .co  m
    }

    System.out.println(count);
}

From source file:Main.java

public static void main(String[] args) {

    Path path = Paths.get("C:", "tutorial/Java/JavaFX", "Topic.txt");

    //convert path to File object
    File path_to_file = path.toFile();
    System.out.println("Path to file name: " + path_to_file.getName());
}

From source file:FileDemo.java

public static void main(String args[]) throws Exception {

    // Display constants
    System.out.println("pathSeparatorChar = " + File.pathSeparatorChar);
    System.out.println("separatorChar = " + File.separatorChar);

    // Test some methods
    File file = new File(args[0]);
    System.out.println("getName() = " + file.getName());
    System.out.println("getParent() = " + file.getParent());
    System.out.println("getAbsolutePath() = " + file.getAbsolutePath());
    System.out.println("getCanonicalPath() = " + file.getCanonicalPath());
    System.out.println("getPath() = " + file.getPath());
    System.out.println("canRead() = " + file.canRead());
    System.out.println("canWrite() = " + file.canWrite());
}

From source file:FileDemo.java

public static void main(String args[]) {
    File f1 = new File("/java/COPYRIGHT");
    System.out.println("File Name: " + f1.getName());
    System.out.println("Path: " + f1.getPath());
    System.out.println("Abs Path: " + f1.getAbsolutePath());
    System.out.println("Parent: " + f1.getParent());
    System.out.println(f1.exists() ? "exists" : "does not exist");
    System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
    System.out.println(f1.canRead() ? "is readable" : "is not readable");
    System.out.println("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
    System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
    System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
    System.out.println("File last modified: " + f1.lastModified());
    System.out.println("File size: " + f1.length() + " Bytes");
}

From source file:MainClass.java

public static void main(String[] Args) {
    String filepath = "C:/myFile.txt";
    File aFile = new File(filepath);
    FileOutputStream outputFile = null;
    if (aFile.isFile()) {
        File newFile = aFile;
        do {//from   w ww.j ava 2 s . c  o m
            String name = newFile.getName();
            int period = name.indexOf('.');
            newFile = new File(newFile.getParent(),
                    name.substring(0, period) + "_old" + name.substring(period));
        } while (newFile.exists());
        aFile.renameTo(newFile);
    }
    try {
        outputFile = new FileOutputStream(aFile);
        System.out.println("myFile.txt output stream created");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
}