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:MainClass.java

public static void main(String[] args) throws Exception {
    File fromFile = new File("fromFile.txt");
    File toFile = new File("toFile.txt");
    FileInputStream inFile = new FileInputStream(fromFile);
    FileOutputStream outFile = new FileOutputStream(toFile);
    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();
    int bytesWritten = 0;
    long byteCount = inChannel.size();
    while (bytesWritten < byteCount) {
        bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel);
    }/*from w w w. java  2s  . co  m*/
    inFile.close();
    outFile.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File f = new File("index.html");
    JEditorPane jep = new JEditorPane(f.toURI().toURL());

    JScrollPane sp = new JScrollPane(jep);
    sp.setPreferredSize(new Dimension(400, 200));

    JOptionPane.showMessageDialog(null, sp);
}

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;//w w w . jav  a  2 s  .c  o  m
        do {
            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);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File f = new File("name.txt");

    if (!f.exists()) {
        System.out.println("File not found.");
        return;//from   www  .  j av  a2 s.c  om
    }
    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    long t = Calendar.getInstance().getTimeInMillis();
    if (!f.setLastModified(t))
        System.out.println("Can't set time.");

    if (!f.setReadOnly())
        System.out.println("Can't set to read-only.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
    System.out.println("  Last modified on " + new Date(f.lastModified()));

    if (!f.setWritable(true, false))
        System.out.println("Can't return to read/write.");

    if (f.canRead())
        System.out.println("  Readable");
    else
        System.out.println("  Not Readable");

    if (f.canWrite())
        System.out.println("  Writable");
    else
        System.out.println("  Not Writable");
}

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);//  w w w .ja va 2s.co  m
    chooser.setCurrentDirectory(null);
    chooser.showOpenDialog(null);
    AccessibleContext obj = chooser.getAccessibleContext();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileChannel rwChannel = new RandomAccessFile(new File("test.txt"), "rw").getChannel();
    FileChannel roChannel = new RandomAccessFile(new File("test.txt"), "r").getChannel();

    FileChannel pvChannel = new RandomAccessFile(new File("text.txt"), "rw").getChannel();
    ByteBuffer pvBuf = roChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File file = new File("test.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(file));

    while (reader.getEventType() == 6)
        reader.next();//from   w w w.ja  va 2s  .com

    int eventTypeID = reader.next();
    System.out.println("Hello " + reader.getText());
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File file = new File("text.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(file));

    int eventTypeID = reader.nextTag();

    eventTypeID = reader.nextTag();//from w  w w.  j  a v  a  2  s  .c  o  m

    eventTypeID = reader.next();
    System.out.println("Hello " + reader.getText());
}

From source file:MediaPlayerDemo.java

public static void main(String args[]) {
    Player player;//  ww w  .j a v a 2  s.c om

    File file = new File("yourFile");

    player = Manager.createPlayer(file.toURI().toURL());
    //    player.addControllerListener(new EventHandler());
    player.start(); // start player

    player.close();

    Component visual = player.getVisualComponent();
    Component control = player.getControlPanelComponent();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File aFile = new File("main.java");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer lengthBuf = ByteBuffer.allocate(8);
    while (true) {
        if (inChannel.read(lengthBuf) == -1) {
            break;
        }/* w  ww .j  a  v  a 2  s . c  om*/
        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();
}