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) throws Exception {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");
    FileInputStream fis1 = new FileInputStream(file1);
    FileInputStream fis2 = new FileInputStream(file2);
    int i1 = fis1.read();
    int i2 = fis2.read();
    while (i1 != -1) {
        if (i1 != i2) {
            areFilesIdentical = false;/*from w  w w .ja v  a 2s.  com*/
            break;
        }
        i1 = fis1.read();
        i2 = fis2.read();
    }
    fis1.close();
    fis2.close();
    System.out.println(areFilesIdentical);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    // Create a read-only memory-mapped file
    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();

    ByteBuffer readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());

}

From source file:Main.java

public static void main(String[] argv) {
    String filename = File.separator + "tmp";
    JFileChooser fc = new JFileChooser(new File(filename));

    // Show open dialog
    fc.showOpenDialog(null);//  www  . ja  v  a 2  s .c o  m
    File selFile = fc.getSelectedFile();

    // Show save dialog
    fc.showSaveDialog(null);
    selFile = fc.getSelectedFile();

}

From source file:MainClass.java

public static void main(String[] a) {
    File aFile = new File("charData.txt");
    FileInputStream inFile = null;
    try {/*ww w.  j a  va  2 s . c  o m*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    FileChannel inChannel = inFile.getChannel();
}

From source file:MainClass.java

public static void main(final String[] args) {
    File path = new File(".");
    String[] list;//w w  w.  jav  a2s. co  m
    if (args.length == 0)
        list = path.list();
    else
        list = path.list(new FilenameFilter() {
            private Pattern pattern = Pattern.compile(args[0]);

            public boolean accept(File dir, String name) {
                return pattern.matcher(new File(name).getName()).matches();
            }
        });
    Arrays.sort(list);
    for (int i = 0; i < list.length; i++)
        System.out.println(list[i]);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream(new File("C://test.txt"));

    // skip bytes from file input stream
    fis.skip(4);//w  w  w . j  a  v  a 2 s.c  o  m

    // read bytes from this stream
    int i = fis.read();

    // converts integer to character
    char c = (char) i;
    System.out.print("Character read: " + c);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileOutputStream fos = new FileOutputStream(new File("C:/Demo"));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write("this is a test".getBytes());
    bos.flush();//ww w. j  a  va  2  s. c om
    bos.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw");
    raf.writeInt(1);//www.j  a va2s  .  c  o m
    for (int i = 0; i < 10; i++) {
        raf.seek(raf.length() - 4);
        raf.writeInt(raf.readInt());
    }
    raf.close();

}

From source file:Delete.java

public static void main(String[] args) {
    String fileName = "file.txt";
    // A File object to represent the filename
    File f = new File(fileName);

    // Make sure the file or directory exists and isn't write protected
    if (!f.exists())
        throw new IllegalArgumentException("Delete: no such file or directory: " + fileName);

    if (!f.canWrite())
        throw new IllegalArgumentException("Delete: write protected: " + fileName);

    // If it is a directory, make sure it is empty
    if (f.isDirectory()) {
        String[] files = f.list();
        if (files.length > 0)
            throw new IllegalArgumentException("Delete: directory not empty: " + fileName);
    }/*from  www  .j a  va  2 s.com*/

    // Attempt to delete it
    boolean success = f.delete();

    if (!success)
        throw new IllegalArgumentException("Delete: deletion failed");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ZipFile zipFile = new ZipFile(new File("testfile.zip"), ZipFile.OPEN_READ);

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