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, UnsupportedEncodingException {

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

    // 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();/*from   w  w w  .j a  v  a2s .  co m*/
    System.out.println("Formatter Flushed.");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File archivo = new File("c:/Java_Dev/run.bat");
    FileReader fr = new FileReader(archivo);
    BufferedReader br = new BufferedReader(fr);

    Vector<String> lines = new Vector<String>();

    String line;/*w  w w . j ava  2  s  .c o  m*/
    while ((line = br.readLine()) != null) {
        lines.add(line);
    }
    JOptionPane.showMessageDialog(null, new JScrollPane(new JList(lines)));
    fr.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String phrase = new String("www.java2s.com\n");

    File aFile = new File("test.txt");
    FileOutputStream outputFile = null;
    outputFile = new FileOutputStream(aFile, true);
    System.out.println("File stream created successfully.");

    FileChannel outChannel = outputFile.getChannel();

    ByteBuffer buf = ByteBuffer.allocate(1024);
    System.out.println("New buffer:           position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    // Load the data into the buffer
    for (char ch : phrase.toCharArray()) {
        buf.putChar(ch);//  w ww. j a va 2  s .c om
    }
    System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());
    buf.flip();
    System.out.println("Buffer after flip:    position = " + buf.position() + "\tLimit = " + buf.limit()
            + "\tcapacity = " + buf.capacity());

    outChannel.write(buf);
    outputFile.close();
    System.out.println("Buffer contents written to file.");
}

From source file:Main.java

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

    byte b = 66;/*from   w ww . jav a2s .c  om*/
    int i = 0;
    FileOutputStream fos = new FileOutputStream(new File("C://test1.txt"), true);

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:Main.java

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

    byte b = 66;/*from w w w. j a  v  a 2 s . co m*/
    int i = 0;
    FileOutputStream fos = new FileOutputStream(new File("C://test1.txt"));

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("file.dat");
    FileInputStream inFile = null;

    try {//from w w w.j  ava2  s.com
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(0);
    }
    FileChannel inChannel = inFile.getChannel();
    final int COUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * COUNT);
    long[] data = new long[COUNT];
    try {
        while (inChannel.read(buf) != -1) {
            ((ByteBuffer) (buf.flip())).asLongBuffer().get(data);
            System.out.println();
            for (long prime : data)
                System.out.printf("%10d", prime);
            buf.clear();
        }
        System.out.println("\nEOF reached.");
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File file = new File("filename.txt");
    Icon icon = chooser.getIcon(file);

    JLabel label = new JLabel("" + file);
    label.setIcon(icon);//w w w .  jav  a2s.  c  o m
    JFrame frame = new JFrame();
    frame.getContentPane().add(label, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("primes.txt");
    FileInputStream inFile = null;
    try {/*from   ww w  . ja va2  s  . c  o m*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    try {
        ByteBuffer lengthBuf = ByteBuffer.allocate(8);
        while (true) {
            if (inChannel.read(lengthBuf) == -1) {
                break;
            }
            lengthBuf.flip();
            int strLength = (int) lengthBuf.getDouble();
            ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
            if (inChannel.read(buf) == -1) {
                break;
            }
            buf.flip();
            byte[] strChars = new byte[2 * strLength];
            buf.get(strChars);
            System.out.println(strLength);
            System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
            System.out.println(buf.getLong());
            lengthBuf.clear();
        }
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Main.java

public static void main(String[] a) {
    try {/*from  w w  w  .  j  a  va  2  s.co m*/
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }

        desktop.edit(new File("c:\\a.txt"));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

}

From source file:Test.java

public static void main(String[] a) {
    try {// w  ww .  j a  va2  s .c om
        Desktop desktop = null;
        if (Desktop.isDesktopSupported()) {
            desktop = Desktop.getDesktop();
        }

        desktop.open(new File("c:\\a.doc"));
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

}