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 FileNotFoundException {

    Formatter formatter = new Formatter(new File("generated/format.txt"));

    // format a new string
    String name = "from java2s.com";
    formatter.format("Hello %s !", name);

    // print the formatted string
    System.out.println(formatter);

    // flush the formatter. Here it does nothing.
    formatter.flush();// w  w w. ja va  2 s .  c o m
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("your.ttf");
    FileInputStream in = new FileInputStream(f);
    Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, in);
    Font dynamicFont32Pt = dynamicFont.deriveFont(32f);

    JLabel testLabel = new JLabel(dynamicFont.getName());
    testLabel.setFont(dynamicFont32Pt);/* www .  java  2 s .  c  o m*/
    JFrame frame = new JFrame("Font Loading Demo");
    frame.getContentPane().add(testLabel);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File aFile = new File("charData.xml");
    FileInputStream inFile = null;

    inFile = new FileInputStream(aFile);

    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(48);

    while (inChannel.read(buf) != -1) {
        System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0));
        buf.clear();//from  w  w  w  . j  av a 2  s  . c o m
    }
    inFile.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();/*from ww  w.  j  a v a  2 s.c  o  m*/

    MappedByteBuffer mbb = buf.load();

    channel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("c:\\a.bat");
    InputStream is = new FileInputStream(file);

    long length = file.length();
    if (length > Integer.MAX_VALUE) {
        System.out.println("File is too large");
    }//from   w  w  w .  j a v  a 2  s .  c om

    byte[] bytes = new byte[(int) length];

    int offset = 0;
    int numRead = 0;
    while (numRead >= 0) {
        numRead = is.read(bytes, offset, bytes.length - offset);
        offset += numRead;
    }

    if (offset < bytes.length) {
        throw new IOException("Could not completely read file " + file.getName());
    }
    is.close();
    System.out.println(new String(bytes));
}

From source file:Main.java

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

    File f = new File("your.ttf");
    FileInputStream in = new FileInputStream(f);
    Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, in);
    Font dynamicFont32Pt = dynamicFont.deriveFont(32f);

    JLabel testLabel = new JLabel("Dynamically loaded font \"" + dynamicFont.getName() + "\"");
    testLabel.setFont(dynamicFont32Pt);/*from  w w  w .  j a  va  2  s . c  om*/
    JFrame frame = new JFrame("Font Loading Demo");
    frame.getContentPane().add(testLabel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileChannel rwChannel = new RandomAccessFile(new File("test.txt"), "rw").getChannel();
    ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File(args[0]);
    sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file);
    Icon icon = new ImageIcon(sf.getIcon(true));
    System.out.println("type = " + sf.getFolderType());

    JLabel label = new JLabel(icon);
    JFrame frame = new JFrame();
    frame.getContentPane().add(label);//from www  . j  a  v  a2 s  .  c  o  m
    frame.pack();
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File(".").getCanonicalPath());
    chooser.setCurrentDirectory(f);//from w w  w  .ja  va  2 s . com
    chooser.setCurrentDirectory(null);
    chooser.showOpenDialog(null);
    FileFilter obj = chooser.getAcceptAllFileFilter();
}

From source file:FileAttributesDemo.java

public static void main(String[] args) throws IOException {
    // Create a new file, by default canWrite=true, readonly=false
    File file = new File("test.txt");
    if (file.exists()) {
        file.delete();//from w  w w  . ja  v a 2  s.  c o  m
    }
    file.createNewFile();
    System.out.println("Before. canWrite?" + file.canWrite());

    // set to read-only, atau canWrite = false */
    file.setWritable(false);
    System.out.println("After. canWrite?" + file.canWrite());
}