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[] a) {
    JFrame frame = new JFrame("JFileChooser Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JFileChooser fileChooser = new JFileChooser(".");
    String f = fileChooser.getDescription(new File("."));

    frame.add(fileChooser, BorderLayout.CENTER);

    frame.pack();//  w  w w .  ja  va 2 s  .  c  o  m
    frame.setVisible(true);
}

From source file:CompressIt.java

public static void main(String[] args) {
    String filename = args[0];/*from  ww w.ja v a  2  s . c  o m*/
    try {
        File file = new File(filename);
        int length = (int) file.length();
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
        GZIPOutputStream gos = new GZIPOutputStream(baos);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = bis.read(buffer)) != -1) {
            gos.write(buffer, 0, bytesRead);
        }
        bis.close();
        gos.close();
        System.out.println("Input Length: " + length);
        System.out.println("Output Length: " + baos.size());
    } catch (FileNotFoundException e) {
        System.err.println("Invalid Filename");
    } catch (IOException e) {
        System.err.println("I/O Exception");
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;/* ww  w .  j a  v a2 s.c o m*/
    db = dbf.newDocumentBuilder();
    Document doc = db.parse(new File("games.xml"));
}

From source file:Main.java

public static void main(String[] args) {

    // create a new list of arguments for our process
    String[] list = { "notepad.exe", "test.txt" };

    // create the process builder
    ProcessBuilder pb = new ProcessBuilder(list);
    try {/*from   w  w  w . ja  v a2 s .  c  o  m*/

        pb = pb.redirectError(ProcessBuilder.Redirect.from(new File("c:/")));
        pb.start();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    // create a new list of arguments for our process
    String[] list = { "notepad.exe", "test.txt" };

    // create the process builder
    ProcessBuilder pb = new ProcessBuilder(list);
    try {//  www  . j a v  a 2  s  . co  m

        pb = pb.redirectOutput(ProcessBuilder.Redirect.from(new File("c:/")));
        pb.start();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

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

    File xml = new File("yourFile.xml");

    Document doc = (Document) new SAXBuilder().build(xml);
    Element rootNode = doc.getRootElement();
    List list = rootNode.getChildren("staff");
    XMLOutputter xmlOut = new XMLOutputter();

    for (int i = 0; i < list.size(); i++) {

        Element node = (Element) list.get(i);
        if (node.getChildText("firstname").equals("sanjay"))
            node.getChild("salary").getChild("basic").setText("250000");
        xmlOut.setFormat(Format.getPrettyFormat());
        xmlOut.output(doc, new FileWriter("yourFile.xml"));
    }/*  ww w  . j  a va  2s .  com*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    File outputFile = new File("jsoutput.txt");
    System.out.println("Script output will be written to " + outputFile.getAbsolutePath());

    FileWriter writer = new FileWriter(outputFile);
    ScriptContext defaultCtx = engine.getContext();
    defaultCtx.setWriter(writer);/*from w  w w  .j  ava2 s . co m*/
    String script = "print('Hello custom output writer')";
    engine.eval(script);
    writer.close();

}

From source file:Main.java

  public static void main(String[] args) throws IOException {
  ServerSocket servsock = new ServerSocket(123456);
  File myFile = new File("s.pdf");
  while (true) {//from  w w  w .  j a v a  2 s  . c  om
    Socket sock = servsock.accept();
    byte[] mybytearray = new byte[(int) myFile.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);
    os.flush();
    sock.close();
  }
}

From source file:ExtensionFilter.java

public static void main(String args[]) {
    String dirname = "/java";
    File f1 = new File(dirname);
    FilenameFilter only = new ExtensionFilter("html");
    String s[] = f1.list(only);/*w  w w.j  a v  a2  s.c  o m*/
    for (int i = 0; i < s.length; i++) {
        System.out.println(s[i]);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from w w  w .  ja v  a2s . c  o 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());
    }
}