Example usage for java.io File File

List of usage examples for java.io File File

Introduction

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

Prototype

public File(URI uri) 

Source Link

Document

Creates a new File instance by converting the given file: URI into an abstract pathname.

Usage

From source file:Main.java

public static void main(String[] args) {
    File f = new File("c:/test");
    FilenameFilter fileNameFilter = new FilenameFilter() {
        @Override/*w w w .j ava  2s  .  c  om*/
        public boolean accept(File dir, String name) {
            if (name.lastIndexOf('.') > 0) {
                int lastIndex = name.lastIndexOf('.');
                String str = name.substring(lastIndex);
                if (str.equals(".txt")) {
                    return true;
                }
            }
            return false;
        }
    };
    File[] paths = f.listFiles(fileNameFilter);
    for (File path : paths) {
        System.out.println(path);
    }
}

From source file:Main.java

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

    // create new files
    File f = new File("test.txt");

    // create new file in the system
    f.createNewFile();//w  w  w  . j a v a 2  s .  com

    // tests if file exists
    boolean bool = f.exists();

    System.out.println("File exists: " + bool);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("C:/String.txt");
    StringBuffer strContent = new StringBuffer();
    FileInputStream fin = new FileInputStream(file);
    int ch;/*from ww  w.java  2 s. c  o m*/
    while ((ch = fin.read()) != -1) {
        strContent.append((char) ch);
    }
    fin.close();
    System.out.println(strContent);
}

From source file:Main.java

public static void main(String[] args) {

    // returns pathnames for files and directory
    File f = new File("C:/Texts/a/b/c");

    // create directories
    boolean bool = f.mkdirs();

    System.out.print("Directory created? " + bool);

}

From source file:Main.java

public static void main(String[] args) {

    // returns pathnames for files and directory
    File f = new File("C:/Texts");

    // create//from  w ww.  j a  va  2 s.com
    boolean bool = f.mkdir();

    System.out.print("Directory created? " + bool);

}

From source file:Main.java

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

    int ch;/*from  w  w  w.j  a v a2  s.  c om*/
    // skip first 10 bytes
    fin.skip(10);
    while ((ch = fin.read()) != -1) {
        System.out.print((char) ch);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  w w .j ava2s. c o  m*/
        PrintWriter pw = new PrintWriter(new File("c:/text.txt"), "ACSII");

        // append chars
        pw.append('H');
        pw.append('e');
        pw.append('l');
        pw.append('l');
        pw.append('o');

        // flush the writer
        pw.flush();
        pw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//  w w  w . j  ava 2s .  co m
        PrintWriter pw = new PrintWriter(new File("c:/text.txt"));

        // append chars
        pw.append('H');
        pw.append('e');
        pw.append('l');
        pw.append('l');
        pw.append('o');

        // flush the writer
        pw.flush();
        pw.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("data.txt");
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();

        Scanner lineScanner = new Scanner(line);
        lineScanner.useDelimiter(",");
        while (lineScanner.hasNext()) {
            String part = lineScanner.next();
            System.out.print(part + ", ");
        }/* w w  w.  j  a va2  s.  co  m*/
        System.out.println();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File fileToChange = new File("C:/myfile.txt");

    Date filetime = new Date(fileToChange.lastModified());
    System.out.println(filetime.toString());

    System.out.println(fileToChange.setLastModified(System.currentTimeMillis()));

    filetime = new Date(fileToChange.lastModified());
    System.out.println(filetime.toString());

}