Example usage for java.io FileReader FileReader

List of usage examples for java.io FileReader FileReader

Introduction

In this page you can find the example usage for java.io FileReader FileReader.

Prototype

public FileReader(FileDescriptor fd) 

Source Link

Document

Creates a new FileReader , given the FileDescriptor to read, using the platform's java.nio.charset.Charset#defaultCharset() default charset .

Usage

From source file:Main.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    conn.setAutoCommit(false);/*from ww  w .j  a  va2s .  c  om*/

    String sql = "INSERT INTO documents (name, description, data) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "a.txt");
    stmt.setString(2, "b");

    File data = new File("C:\\a.txt");
    FileReader reader = new FileReader(data);
    stmt.setCharacterStream(3, reader, (int) data.length());
    stmt.execute();

    conn.commit();
    reader.close();
    conn.close();

}

From source file:GZIPcompress.java

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.out.println("Usage: \nGZIPcompress file\n" + "\tUses GZIP compression to compress "
                + "the file to test.gz");
        System.exit(1);/* ww w  .  j  a  v  a2  s. co  m*/
    }
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")));
    System.out.println("Writing file");
    int c;
    while ((c = in.read()) != -1)
        out.write(c);
    in.close();
    out.close();
    System.out.println("Reading file");
    BufferedReader in2 = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));
    String s;
    while ((s = in2.readLine()) != null)
        System.out.println(s);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval(new FileReader("res/nashorn2.js"));
}

From source file:Main.java

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

    Document newDoc = domBuilder.newDocument();
    Element rootElement = newDoc.createElement("CSV2XML");
    newDoc.appendChild(rootElement);/*w w  w  . j  a v  a 2s.  c  o m*/

    BufferedReader csvReader = new BufferedReader(new FileReader("csvFileName.txt"));
    String curLine = csvReader.readLine();
    String[] csvFields = curLine.split(",");
    Element rowElement = newDoc.createElement("row");
    for (String value : csvFields) {
        Element curElement = newDoc.createElement(value);
        curElement.appendChild(newDoc.createTextNode(value));
        rowElement.appendChild(curElement);
        rootElement.appendChild(rowElement);
    }
    csvReader.close();
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();
    Source src = new DOMSource(newDoc);
    Result dest = new StreamResult(new File("xmlFileName"));
    aTransformer.transform(src, dest);
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    File documentFile = new File(args[0]);
    File schemaFile = new File(args[1]);

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = null;/*from   ww  w  . ja va 2s .c o  m*/
    try {
        schema = factory.newSchema(schemaFile);
    } catch (SAXException e) {
        fail(e);
    }

    Validator validator = schema.newValidator();

    SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile)));

    try {
        validator.validate(source);
    } catch (SAXException e) {
        fail(e);
    }
}

From source file:chat.jsonTest.java

/**
 * @param args the command line arguments
 *//*ww  w  . j a va 2s. com*/
public static void main(String[] args) throws ParseException, IOException {
    // TODO code application logic here
    JSONParser parser = new JSONParser();
    String fullPath = "userInfo.json";
    //BufferedReader reader = new BufferedReader(new FileReader(fullPath));

    Object obj = parser.parse(new FileReader("userInfo.json"));
    JSONObject jsonObject = (JSONObject) obj;
    /*while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }*/
    JSONArray lang = (JSONArray) jsonObject.get("userInfo");
    Iterator i = lang.iterator();

    // take each value from the json array separately
    while (i.hasNext()) {
        JSONObject innerObj = (JSONObject) i.next();
        System.out
                .println("username " + innerObj.get("username") + " with password " + innerObj.get("password"));
    }

}

From source file:CompRcv.java

public static void main(String[] args) throws Exception {
    Socket sock = new Socket(args[0], Integer.parseInt(args[1]));
    GZIPOutputStream zip = new GZIPOutputStream(sock.getOutputStream());
    String line;//from ww w  .ja va  2s.  co m
    BufferedReader bis = new BufferedReader(new FileReader(args[2]));
    while (true) {
        try {
            line = bis.readLine();
            if (line == null)
                break;
            line = line + "\n";
            zip.write(line.getBytes(), 0, line.length());
        } catch (Exception e) {
            break;
        }
    }
    zip.finish();
    zip.close();
    sock.close();
}

From source file:DataFileTest.java

public static void main(String[] args) {
    Employee staff = new Employee("Java Source", 35500);

    staff.raiseSalary(5.25);//from w w w. j ava2  s .  c  o  m

    try {
        PrintWriter out = new PrintWriter(new FileWriter("employee.dat"));
        writeData(staff, out);
        out.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }

    try {
        BufferedReader in = new BufferedReader(new FileReader("employee.dat"));
        Employee e = readData(in);
        e.print();
        in.close();
    } catch (IOException e) {
        System.out.print("Error: " + e);
        System.exit(1);
    }
}

From source file:dependencies.DependencyResolving.java

/**
 * @param args the command line arguments
 *//*from  w w  w  .j av a  2 s .  c  o m*/
public static void main(String[] args) {
    // TODO code application logic here
    JSONParser parser = new JSONParser(); //we use JSONParser in order to be able to read from JSON file
    try { //here we declare the file reader and define the path to the file dependencies.json
        Object obj = parser.parse(new FileReader(
                "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\dependencies.json"));
        JSONObject project = (JSONObject) obj; //a JSON object containing all the data in the .json file
        JSONArray dependencies = (JSONArray) project.get("dependencies"); //get array of objects with key "dependencies"
        System.out.print("We need to install the following dependencies: ");
        Iterator<String> iterator = dependencies.iterator(); //define an iterator over the array "dependencies"
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        } //on the next line we declare another object, which parses a Parser object and reads from all_packages.json
        Object obj2 = parser.parse(new FileReader(
                "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\all_packages.json"));
        JSONObject tools = (JSONObject) obj2; //a JSON object containing all thr data in the file all_packages.json
        for (int i = 0; i < dependencies.size(); i++) {
            if (tools.containsKey(dependencies.get(i))) {
                System.out.println(
                        "In order to install " + dependencies.get(i) + ", we need the following programs:");
                JSONArray temporaryArray = (JSONArray) tools.get(dependencies.get(i)); //a temporary JSON array in which we store the keys and values of the dependencies
                for (i = 0; i < temporaryArray.size(); i++) {
                    System.out.println(temporaryArray.get(i));
                }
                ArrayList<Object> arraysOfJsonData = new ArrayList<Object>(); //an array in which we will store the keys of the objects, after we use the values and won't need them anymore
                for (i = 0; i < temporaryArray.size(); i++) {
                    System.out.println("Installing " + temporaryArray.get(i));
                }
                while (!temporaryArray.isEmpty()) {

                    for (Object element : temporaryArray) {

                        if (tools.containsKey(element)) {
                            JSONArray secondaryArray = (JSONArray) tools.get(element); //a temporary array within the scope of the if-statement
                            if (secondaryArray.size() != 0) {
                                System.out.println("In order to install " + element + ", we need ");
                            }
                            for (i = 0; i < secondaryArray.size(); i++) {
                                System.out.println(secondaryArray.get(i));
                            }

                            for (Object o : secondaryArray) {

                                arraysOfJsonData.add(o);
                                //here we create a file with the installed dependency
                                File file = new File(
                                        "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\installed_modules\\"
                                                + o);
                                if (file.createNewFile()) {
                                    System.out.println(file.getName() + " is installed!");
                                } else {
                                }
                            }
                            secondaryArray.clear();
                        }
                    }
                    temporaryArray.clear();
                    for (i = 0; i < arraysOfJsonData.size(); i++) {
                        temporaryArray.add(arraysOfJsonData.get(i));
                    }
                    arraysOfJsonData.clear();
                }
            }
        }
        Set<String> keys = tools.keySet(); // here we define a set of keys of the objects in all_packages.json
        for (String s : keys) {
            File file = new File(
                    "C:\\Users\\Vladimir\\Documents\\NetBeansProjects\\DependenciesResolving\\src\\dependencies\\installed_modules\\"
                            + s);
            if (file.createNewFile()) {
                System.out.println(file.getName() + " is installed.");
            } else {
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(DependencyResolving.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(DependencyResolving.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:bariopendatalab.ImportData.java

/**
 * @param args the command line arguments
 *//*  w  w  w .j av a2  s .c  om*/
public static void main(String[] args) {
    int i = 0;
    try {
        MongoClient client = new MongoClient("localhost", 27017);
        DBAccess dbaccess = new DBAccess(client);
        dbaccess.dropDB();
        dbaccess.createDB();
        FileReader reader = new FileReader(new File(args[0]));
        Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader().withIgnoreEmptyLines().withDelimiter(',')
                .parse(reader);
        i = 2;
        for (CSVRecord record : records) {
            String type = record.get("Tipologia");
            if (type == null || type.length() == 0) {
                Logger.getLogger(ImportData.class.getName()).log(Level.WARNING, "No type in line {0}", i);
            }

            String description = record.get("Nome");
            if (description != null && description.length() == 0) {
                description = null;
            }

            String address = record.get("Indirizzo");
            if (address != null && address.length() == 0) {
                address = null;
            }

            String civ = record.get("Civ");
            if (civ != null && civ.length() == 0) {
                civ = null;
            }

            if (address != null && civ != null) {
                address += ", " + civ;
            }

            String note = record.get("Note");
            if (note != null && note.length() == 0) {
                note = null;
            }

            String longitudine = record.get("Longitudine");
            String latitudine = record.get("Latitudine");
            if (longitudine != null && latitudine != null) {
                if (longitudine.length() > 0 && latitudine.length() > 0) {
                    try {
                        dbaccess.insert(type, description, address, note, Utils
                                .get2DPoint(Double.parseDouble(latitudine), Double.parseDouble(longitudine)));
                    } catch (NumberFormatException nex) {
                        dbaccess.insert(type, description, address, note, null);
                    }
                } else {
                    dbaccess.insert(type, description, address, note, null);
                }
            } else {
                dbaccess.insert(type, description, address, note, null);
            }
            i++;
        }
        reader.close();
    } catch (Exception ex) {
        Logger.getLogger(ImportData.class.getName()).log(Level.SEVERE, "Error line " + i, ex);
    }
}