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) {
    try {//from  ww  w  . j  a va 2 s  .  co m
        char[] chars = new char[2];
        chars[0] = '\u4F60';
        chars[1] = '\u597D';
        String encoding = "GB18030";
        File textFile = new File("C:\\temp\\myFile.txt");
        PrintWriter writer = new PrintWriter(textFile,

                encoding);
        writer.write(chars);
        writer.close();

        // read back
        InputStreamReader reader = new InputStreamReader(new FileInputStream(textFile), encoding);
        char[] chars2 = new char[2];
        reader.read(chars2);
        System.out.print(chars2[0]);
        System.out.print(chars2[1]);
        reader.close();
    } catch (IOException e) {
        System.out.println(e.toString());
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    File f1 = new File("MainClass.java");
    System.out.println("File Name:" + f1.getName());
    System.out.println("Path:" + f1.getPath());
    System.out.println("Abs Path:" + f1.getAbsolutePath());
    System.out.println("Parent:" + f1.getParent());
    System.out.println(f1.exists() ? "exists" : "does not exist");
    System.out.println(f1.canWrite() ? "is writeable" : "is not writeable");
    System.out.println(f1.canRead() ? "is readable" : "is not readable");
    System.out.println("is a directory" + f1.isDirectory());
    System.out.println(f1.isFile() ? "is normal file" : "might be a named pipe");
    System.out.println(f1.isAbsolute() ? "is absolute" : "is not absolute");
    System.out.println("File last modified:" + f1.lastModified());
    System.out.println("File size:" + f1.length() + " Bytes");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));

    int eventTypeID = reader.nextTag();
    reader.require(XMLStreamConstants.START_ELEMENT, null, "person");

    eventTypeID = reader.nextTag();//  w  w w.  j av  a 2  s  . c o  m
    reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    System.out.println(reader.getElementText());

}

From source file:MainClass.java

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

    try {/*from  w ww.j a  va2s .  c o m*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }

    FileChannel inChannel = inFile.getChannel();
    final int COUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * COUNT);
    long[] data = new long[COUNT];
    try {
        int pos = 0;
        while (inChannel.read(buf) != -1) {
            try {
                ((ByteBuffer) (buf.flip())).asLongBuffer().get(data);
                pos = data.length;
            } catch (BufferUnderflowException e) {
                LongBuffer longBuf = buf.asLongBuffer();
                pos = longBuf.remaining();
                longBuf.get(data, 0, pos);
            }
            System.out.println();
            for (int i = 0; i < pos; i++) {
                System.out.printf("%10d", data[i]);
            }
            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 args[]) throws Exception {
    FileInputStream fileInputStream = new FileInputStream(new File("src/file.xml"));
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document doc1 = builder.parse(fileInputStream);
    doc1.getDocumentElement().normalize();
    NodeList kList1 = doc1.getElementsByTagName("item");

    StringBuilder stringBuilder = new StringBuilder();

    for (int temp = 0; temp < kList1.getLength(); temp++) {
        Node kNode1 = kList1.item(temp);
        System.out.println("\nCurrent Element :" + kNode1.getNodeName());
        if (kNode1.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) kNode1;
            System.out.println("node name" + eElement.getNodeName());
            Node in = eElement.getFirstChild();
            if ((in.getTextContent() != null) && !(in.getTextContent()).isEmpty()
                    && !(in.getTextContent().length() == 0))
                stringBuilder.append(in.getTextContent());
        }/*from w  w  w. ja v a2 s .co  m*/
    }
    System.out.println(stringBuilder);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from ww w . j a va2s .  c o  m*/
    factory.setExpandEntityReferences(false);
    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Element element = doc.getElementById("key1");

    String attrValue = element.getAttribute("value");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document originalDocument = db.parse(new File("input.xml"));
    Node originalRoot = originalDocument.getDocumentElement();

    Document copiedDocument = db.newDocument();
    Node copiedRoot = copiedDocument.importNode(originalRoot, true);
    copiedDocument.appendChild(copiedRoot);

}

From source file:Main.java

static public void main(String args[]) throws Exception {
    File infile = new File("inFilename");
    File outfile = new File("outFilename");

    RandomAccessFile inraf = new RandomAccessFile(infile, "r");
    RandomAccessFile outraf = new RandomAccessFile(outfile, "rw");

    FileChannel finc = inraf.getChannel();
    FileChannel foutc = outraf.getChannel();

    MappedByteBuffer inmbb = finc.map(FileChannel.MapMode.READ_ONLY, 0, (int) infile.length());

    Charset inCharset = Charset.forName("UTF8");
    Charset outCharset = Charset.forName("UTF16");

    CharsetDecoder inDecoder = inCharset.newDecoder();
    CharsetEncoder outEncoder = outCharset.newEncoder();

    CharBuffer cb = inDecoder.decode(inmbb);
    ByteBuffer outbb = outEncoder.encode(cb);

    foutc.write(outbb);//from  ww w.jav a2 s .c om

    inraf.close();
    outraf.close();
}

From source file:ReadBinaryFile.java

public static void main(String[] args) throws Exception {
    NumberFormat cf = NumberFormat.getCurrencyInstance();

    File file = new File("product.dat");
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));

    boolean eof = false;
    while (!eof) {
        Product movie = readMovie(in);/*from   w  ww . j a  v a2  s  .  c  o m*/
        if (movie == null)
            eof = true;
        else {
            String msg = Integer.toString(movie.year);
            msg += ": " + movie.title;
            msg += " (" + cf.format(movie.price) + ")";
            System.out.println(msg);
        }
    }
    in.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
    //    stream = AudioSystem.getAudioInputStream(new URL(
    //      "http://hostname/audiofile"));

    AudioFormat format = stream.getFormat();
    if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
        format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(),
                format.getSampleSizeInBits() * 2, format.getChannels(), format.getFrameSize() * 2,
                format.getFrameRate(), true); // big endian
        stream = AudioSystem.getAudioInputStream(format, stream);
    }/*from w w w  .  ja  v  a 2  s  .  co m*/

    SourceDataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(),
            ((int) stream.getFrameLength() * format.getFrameSize()));
    SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(stream.getFormat());
    line.start();

    int numRead = 0;
    byte[] buf = new byte[line.getBufferSize()];
    while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
        int offset = 0;
        while (offset < numRead) {
            offset += line.write(buf, offset, numRead - offset);
        }
    }
    line.drain();
    line.stop();
}