Example usage for java.io FileReader read

List of usage examples for java.io FileReader read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:jeplus.TRNSYSWinTools.java

/**
 * Copy a file from one location to another
 *
 * @param from The source file to be copied
 * @param to The target file to write/*from  w  ww .  j a  va 2s . c om*/
 * @return Successful or not
 */
public static boolean fileCopy(String from, String to) {
    boolean success = true;
    try {
        FileReader in = new FileReader(from);
        FileWriter out = new FileWriter(to);
        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
        }
        in.close();
        out.close();
    } catch (Exception ee) {
        logger.error("Error copying " + from + " to " + to, ee);
        success = false;
    }
    return success;
}

From source file:eu.europa.ec.markt.dss.signature.StreamDocumentTest.java

@Test
public void save() throws Exception {
    document.save("streamDocumentSaveTest.txt");
    assertTrue(Files.exists(Paths.get("streamDocumentSaveTest.txt")));

    FileReader fileReader = new FileReader("streamDocumentSaveTest.txt");
    int read = fileReader.read();
    fileReader.close();/*from w w w . j  a v  a  2s .co  m*/

    assertEquals(65, read);
    Files.deleteIfExists(Paths.get("streamDocumentSaveTest.txt"));
}

From source file:com.linkedin.pinot.tools.admin.command.StopProcessCommand.java

private boolean stopProcess(String fileName) throws IOException {
    File file = new File(fileName);
    FileReader reader = new FileReader(file);
    int pid = reader.read();

    Runtime.getRuntime().exec("kill " + pid);
    reader.close();//from w ww  .ja  va  2 s  .co  m

    file.delete();
    return true;
}

From source file:TestSecurity.java

/**
 * put your documentation comment here// w w  w.  j  av  a 2  s .co m
 * @param req
 * @param res
 * @exception ServletException, IOException
 */
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    out.println("<HTML>");
    out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<BIG>Test Security</BIG>");
    try {
        out.println(h2o + "Information..." + h2c);
        out.println("  Security Manager: " + getSecurityManager().getClass().getName() + p);
        out.println("  ClassLoader: " + this.getClass().getClassLoader() + p);
        //            weblogic.utils.classloaders.GenericClassLoader gcl = (weblogic.utils.classloaders.GenericClassLoader)this.getClass().getClassLoader();
        //            gcl.setDebug( true );
        out.println("  CodeSource: " + this.getClass().getProtectionDomain().getCodeSource().getLocation() + p);
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    /*
     try
     {
     out.println( h2o + "Trying some dangerous J2EE calls..." + h2c );
     String hack = request.getParameter( "hack" );
     Cookie[] cookies = request.getCookies();
     out.println( " -- allowed -- " + p );
     int x = 1 + 2 + 3;
     out.println( hack );  // use it
     int y = 1 + 2 + 3;
     out.println( cookies );  // use it
     String m = "COOKIE: " + cookies[0]; // use it again
     cookies = new Cookie[10]; // reset it
     String n = "COOKIE: " + cookies[5]; // use it again
     }
     catch( Exception e ) { out.println( " -- rejected -- " + e.getMessage() + p ); }
     */
    try {
        out.println(h2o + "Attempting file write to d:/Java..." + h2c);
        File f = new File("d:/Java/blah.txt");
        FileWriter fw = new FileWriter(f);
        fw.write("test\n");
        fw.close();
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting file write to d:/Java/TestServlet..." + h2c);
        File f = new File("d:/Java/TestServlet/blah.txt");
        FileWriter fw = new FileWriter(f);
        fw.write("test\n");
        fw.close();
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting file read to c:/Ntdetect..." + h2c);
        File f = new File("c:/Ntdetect.com");
        FileReader fr = new FileReader(f);
        int c = fr.read();
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting file read to c:/weblogic/weblogic.properties..." + h2c);
        File f = new File("c:/weblogic/weblogic.properties");
        FileReader fr = new FileReader(f);
        int c = fr.read();
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting to connect to yahoo.com..." + h2c);
        Socket s = new Socket("yahoo.com", 8080);
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting to connect to hacker.com..." + h2c);
        Socket s = new Socket("hacker.com", 8080);
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting to listen on port 37337..." + h2c);
        ServerSocket s = new ServerSocket(37337);
        Socket c = s.accept();
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting to listen on port 7001..." + h2c);
        ServerSocket s = new ServerSocket(7001);
        Socket c = s.accept();
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    /*
     try
     {
     out.println( h2o + "Attempting native call..." + h2c );
     native0( 1 );
     out.println( " -- allowed -- " + p );
     }           
     catch( Exception e ) { out.println( " -- rejected -- " + e.getMessage() + p ); }
     */
    try {
        out.println(h2o + "Attempting exec..." + h2c);
        Runtime.getRuntime().exec("dir");
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    try {
        out.println(h2o + "Attempting system exit..." + h2c);
        out.println(" -- allowed -- " + p);
    } catch (Exception e) {
        out.println(" -- rejected -- " + e.getMessage() + p);
    }
    out.println("</BODY></HTML>");
}

From source file:com.krikelin.spotifysource.AppInstaller.java

protected void copyFile(String src, String dest) throws IOException {
    File inputFile = new File(src);
    File outputFile = new File(dest);

    FileReader in = new FileReader(inputFile);
    FileWriter out = new FileWriter(outputFile);
    int c;//  ww w  .  j a  v  a2 s  . c  o  m

    while ((c = in.read()) != -1)
        out.write(c);

    in.close();
    out.close();
}

From source file:pntanasis.base64.base64Test.java

@Test
public void testEncode_File() throws FileNotFoundException, IOException {
    System.out.println("encode (file)");
    base64 instance = new base64();
    FileReader file = new FileReader("test.file"); //test file in project root
    String f = "";
    int c;//from  w w  w .j a  v a 2s  .  c om
    while ((c = file.read()) != -1) {
        f += "" + (char) c;
    }
    String expResult = new String(Base64.encodeBase64(f.getBytes()));
    String result = instance.encode(f);
    assertTrue("Failed, result: " + result + " expexted result: " + expResult, expResult.equals(result));
    System.out.println("encode (file) was successfull");
}

From source file:pntanasis.base64.base64Test.java

@Test
public void testDecode_Performance() throws FileNotFoundException, IOException {
    System.out.println("decode file Performance");
    long start, end;
    base64 instance = new base64();
    FileReader file = new FileReader("test.file"); //test file in project root
    String f = "";
    int c;//from   ww w .ja  va  2s .  c  o m
    while ((c = file.read()) != -1) {
        f += "" + (char) c;
    }
    String base64 = new String(Base64.encodeBase64(f.getBytes()));
    start = System.currentTimeMillis();
    new String(Base64.decodeBase64(base64.getBytes()));
    end = System.currentTimeMillis();
    System.out.println("Process APACHE base64 decode toke " + (end - start) + " MilliSeconds");
    new String(Base64.decodeBase64(base64.getBytes()));
    end = System.currentTimeMillis();
    System.out.println("Process APACHE base64 decode toke " + (end - start) + " MilliSeconds");
    new String(Base64.decodeBase64(base64.getBytes()));
    end = System.currentTimeMillis();
    System.out.println("Process APACHE base64 decode toke " + (end - start) + " MilliSeconds");

    instance.decode(base64);
    end = System.currentTimeMillis();
    System.out.println("Process base64 decode toke " + (end - start) + " MilliSeconds");
    instance.decode(base64);
    end = System.currentTimeMillis();
    System.out.println("Process base64 decode toke " + (end - start) + " MilliSeconds");
    instance.decode(base64);
    end = System.currentTimeMillis();
    System.out.println("Process base64 decode toke " + (end - start) + " MilliSeconds");
}

From source file:pntanasis.base64.base64Test.java

@Test
public void testEncode_Performance() throws FileNotFoundException, IOException {
    System.out.println("encode (file)");
    long start, end;
    base64 instance = new base64();
    FileReader file = new FileReader("test.file"); //test file in project root
    String f = "";
    int c;// ww  w  .  j a  va  2 s .c om
    while ((c = file.read()) != -1) {
        f += "" + (char) c;
    }

    start = System.currentTimeMillis();
    new String(Base64.encodeBase64(f.getBytes()));
    end = System.currentTimeMillis();
    System.out.println("Process APACHE base64 encoding toke " + (end - start) + " MilliSeconds");
    start = System.currentTimeMillis();
    new String(Base64.encodeBase64(f.getBytes()));
    end = System.currentTimeMillis();
    System.out.println("Process APACHE base64 encoding toke " + (end - start) + " MilliSeconds");
    start = System.currentTimeMillis();
    new String(Base64.encodeBase64(f.getBytes()));
    end = System.currentTimeMillis();
    System.out.println("Process APACHE base64 encoding toke " + (end - start) + " MilliSeconds");

    start = System.currentTimeMillis();
    instance.encode(f);
    end = System.currentTimeMillis();
    System.out.println("Process base64 encoding toke " + (end - start) + " MilliSeconds");
    start = System.currentTimeMillis();
    instance.encode(f);
    end = System.currentTimeMillis();
    System.out.println("Process base64 encoding toke " + (end - start) + " MilliSeconds");
    start = System.currentTimeMillis();
    instance.encode(f);
    end = System.currentTimeMillis();
    System.out.println("Process base64 encoding toke " + (end - start) + " MilliSeconds");

    System.out.println("base64 encoding performance test was successfull");
}

From source file:org.ralasafe.userType.UserTypeManagerImpl.java

private String getUserMetadataXML(UserType userType) {
    int ch = 0;/*from w  w  w  .  j ava 2  s. c  om*/
    StringBuffer buf = new StringBuffer();
    try {
        FileReader reader = new FileReader(userType.getSrcFile());
        while ((ch = reader.read()) != -1) {
            buf.append((char) ch);
        }
        reader.close();
        return buf.toString();
    } catch (Exception e) {
        throw new RalasafeException(e);
    }
}

From source file:com.alanjz.spin.project.util.JSONSpinfileParser.java

@Override
public Spinfile parse() {
    StringBuilder stringBuilder;/*from  w  w  w .  j  a v  a  2  s .  co m*/
    FileReader fileReader;
    String source;
    int c;

    try {
        fileReader = new FileReader(getFile().getAbsoluteFile());
        stringBuilder = new StringBuilder();
        while ((c = fileReader.read()) != -1)
            stringBuilder.append((char) c);
        source = stringBuilder.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    setConfig(new JSONObject(source));
    String modelVersion = getConfig().getString("modelVersion");
    String projectGroup = getConfig().getString("projectGroup");
    String projectName = getConfig().getString("projectName");
    String projectVersion = getConfig().getString("projectVersion");

    JSONArray peersArray = getConfig().getJSONArray("peers");
    Peer[] peers = new Peer[peersArray.length()];

    for (int i = 0; i < peers.length; i++) {
        peers[i] = parsePeer(peersArray.getJSONObject(i));
    }

    return new Spinfile(modelVersion, projectGroup, projectName, projectVersion, peers);
}