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

public static JComponent makeExamplePane() {
    JTextArea text = new JTextArea();
    try {//from  w ww .  ja v  a  2s. co  m
        text.read(new FileReader("MetalModExample.java"), null);
    } catch (IOException ex) {
    }

    JScrollPane scroll = new JScrollPane(text);
    return scroll;
}

From source file:Main.java

public static String readLine(String filename) {
    BufferedReader br = null;/*w w  w . j a va 2 s  .co  m*/
    String line = null;
    try {
        br = new BufferedReader(new FileReader(filename), 1024);
        line = br.readLine();
    } catch (IOException e) {
        return null;
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
    return line;
}

From source file:adviewer.util.JSONIO.java

/**
 * Take in file and return one string of json data
 * /*  www  .  j  a  v a2  s  . co  m*/
 * @return
 * @throws IOException
 */
public static String readJSONFile(File inFile) {
    String readFile = "";

    try {
        File fileIn = inFile;

        //if the file is not there then create the file
        if (fileIn.createNewFile()) {
            System.out.println(fileIn + " was created ");
        }

        FileReader fr = new FileReader(fileIn);
        Scanner sc = new Scanner(fr);

        while (sc.hasNext()) {
            readFile += sc.nextLine().trim();
        }

        return readFile;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // System.out.println( readFile  );
    return readFile;
}

From source file:Main.java

public static XMLStreamReader createXMLStreamReader(File file)
        throws FileNotFoundException, XMLStreamException {

    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (factory.isPropertySupported("javax.xml.stream.isValidating")) {
        factory.setProperty("javax.xml.stream.isValidating", Boolean.TRUE);
    }//  w ww.j  a va 2 s  .c o m

    XMLStreamReader reader = factory.createXMLStreamReader(new FileReader(file));

    return reader;
}

From source file:Main.java

public static int countSubstringOccurrence(File file, String substring) throws IOException {
    int count = 0;
    FileReader input = null;/*from w w w  . ja v  a  2 s.c  om*/
    try {
        int currentSubstringIndex = 0;
        char[] buffer = new char[4096];

        input = new FileReader(file);
        int numRead = input.read(buffer);
        while (numRead != -1) {

            for (char c : buffer) {
                if (c == substring.charAt(currentSubstringIndex)) {
                    currentSubstringIndex++;
                    if (currentSubstringIndex == substring.length()) {
                        count++;
                        currentSubstringIndex = 0;
                    }
                } else {
                    currentSubstringIndex = 0;
                }
            }
            numRead = input.read(buffer);
        }
    } finally {
        closeQuietly(input);
    }
    return count;
}

From source file:com.olhcim.engine.Mesh.java

public static Mesh loadMeshFile(String fileName) {
    //load and parse file
    JSONParser parser = new JSONParser();
    Object obj = null;//from   w  w  w .  ja va 2 s .  c  o  m
    try {
        obj = parser.parse(new FileReader(AppMain.class.getResource(fileName).getFile().replace("%20", " ")));
    } catch (IOException ex) {
    } catch (ParseException ex) {
    }
    JSONObject jObj = (JSONObject) obj;

    //load and convert to double array
    Object[] verticesObj = ((JSONArray) jObj.get("positions")).toArray();
    double[] vertices = new double[verticesObj.length];
    for (int i = 0; i < verticesObj.length; i++) {
        vertices[i] = (double) verticesObj[i];
    }

    //load and convert to long array
    Object[] indicesObj = ((JSONArray) jObj.get("indices")).toArray();
    long[] indices = new long[indicesObj.length];
    for (int i = 0; i < indicesObj.length; i++) {
        indices[i] = (long) indicesObj[i];
    }

    int verticesCount = vertices.length / 3;
    int facesCount = indices.length / 3;

    System.out.println("Vertices: " + verticesCount + " Faces: " + facesCount);

    Mesh mesh = new Mesh("", verticesCount, facesCount);

    // Filling the Vertices array of our mesh first
    for (int index = 0; index < verticesCount; index++) {
        float x = (float) vertices[index * 3];
        float y = (float) vertices[index * 3 + 1];
        float z = (float) vertices[index * 3 + 2];
        mesh.vertices[index] = new Vector4f(x, y, z, 1);
    }

    // Then filling the Faces array
    for (int index = 0; index < facesCount; index++) {
        float a = (int) indices[index * 3];
        float b = (int) indices[index * 3 + 1];
        float c = (int) indices[index * 3 + 2];

        mesh.faces[index] = new Vector3f(a, b, c);
    }

    return mesh;
}

From source file:com.linkedin.pinot.perf.ForwardIndexWriterBenchmark.java

public static void convertRawToForwardIndex(File rawFile) throws Exception {
    List<String> lines = IOUtils.readLines(new FileReader(rawFile));
    int totalDocs = lines.size();
    int max = Integer.MIN_VALUE;
    int maxNumberOfMultiValues = Integer.MIN_VALUE;
    int totalNumValues = 0;
    int data[][] = new int[totalDocs][];
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        String[] split = line.split(",");
        totalNumValues = totalNumValues + split.length;
        if (split.length > maxNumberOfMultiValues) {
            maxNumberOfMultiValues = split.length;
        }//  w  w w  .j  av a  2 s  .c  o m
        data[i] = new int[split.length];
        for (int j = 0; j < split.length; j++) {
            String token = split[j];
            int val = Integer.parseInt(token);
            data[i][j] = val;
            if (val > max) {
                max = val;
            }
        }
    }
    int maxBitsNeeded = (int) Math.ceil(Math.log(max) / Math.log(2));
    int size = 2048;
    int[] offsets = new int[size];
    int bitMapSize = 0;
    File outputFile = new File("output.mv.fwd");

    FixedBitMultiValueWriter fixedBitSkipListSCMVWriter = new FixedBitMultiValueWriter(outputFile, totalDocs,
            totalNumValues, maxBitsNeeded);

    for (int i = 0; i < totalDocs; i++) {
        fixedBitSkipListSCMVWriter.setIntArray(i, data[i]);
        if (i % size == size - 1) {
            MutableRoaringBitmap rr1 = MutableRoaringBitmap.bitmapOf(offsets);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            rr1.serialize(dos);
            dos.close();
            // System.out.println("Chunk " + i / size + " bitmap size:" + bos.size());
            bitMapSize += bos.size();
        } else if (i == totalDocs - 1) {
            MutableRoaringBitmap rr1 = MutableRoaringBitmap.bitmapOf(Arrays.copyOf(offsets, i % size));
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            rr1.serialize(dos);
            dos.close();
            // System.out.println("Chunk " + i / size + " bitmap size:" + bos.size());
            bitMapSize += bos.size();
        }
    }
    fixedBitSkipListSCMVWriter.close();
    System.out.println("Output file size:" + outputFile.length());
    System.out.println("totalNumberOfDoc\t\t\t:" + totalDocs);
    System.out.println("totalNumberOfValues\t\t\t:" + totalNumValues);
    System.out.println("chunk size\t\t\t\t:" + size);
    System.out.println("Num chunks\t\t\t\t:" + totalDocs / size);
    int numChunks = totalDocs / size + 1;
    int totalBits = (totalNumValues * maxBitsNeeded);
    int dataSizeinBytes = (totalBits + 7) / 8;

    System.out.println("Raw data size with fixed bit encoding\t:" + dataSizeinBytes);
    System.out.println("\nPer encoding size");
    System.out.println();
    System.out.println("size (offset + length)\t\t\t:" + ((totalDocs * (4 + 4)) + dataSizeinBytes));
    System.out.println();
    System.out.println("size (offset only)\t\t\t:" + ((totalDocs * (4)) + dataSizeinBytes));
    System.out.println();
    System.out.println("bitMapSize\t\t\t\t:" + bitMapSize);
    System.out.println("size (with bitmap)\t\t\t:" + (bitMapSize + (numChunks * 4) + dataSizeinBytes));

    System.out.println();
    System.out.println("Custom Bitset\t\t\t\t:" + (totalNumValues + 7) / 8);
    System.out.println("size (with custom bitset)\t\t\t:"
            + (((totalNumValues + 7) / 8) + (numChunks * 4) + dataSizeinBytes));
}

From source file:Main.java

static long getLineCountForFile(File file) throws IOException {
    LineNumberReader in = new LineNumberReader(new FileReader(file));
    long numLines = 0;
    while (in.readLine() != null)
        ;/*from  w w w.  j  a v a2s.c  o  m*/
    numLines = in.getLineNumber();
    in.close();

    return numLines;

}

From source file:Main.java

/** Forms an <code>InputSource</code> for parsing XML documents
 *  from a file. Assumes a default character encoding. Returns
 *  <code>null</code> if any of the exceptions is thrown.
 *
 * @param file The file./*from   www . ja v a2  s  .co  m*/
 * @return An <code>InputSource</code> representation.
 */
public static InputSource getInputSource(File file) {
    InputSource retVal = null;
    try {
        retVal = new InputSource(new BufferedReader(new FileReader(file)));
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
    return retVal;
}

From source file:atualizador.Atualizador.java

private static void VerificarAtualizacao(String MD5FileName, String SistemaFileName, String DiretorioFTP) {
    try {//from w  ww.ja v a  2 s.c  om

        FTPDownload(MD5FileName, DiretorioFTP); //Arquivo TXT com o MD5

        //Le o valor do arquivo
        FileReader arq = new FileReader(MD5FileName);

        BufferedReader lerArq = new BufferedReader(arq);
        String linha = lerArq.readLine();

        String Md5FPT = linha;
        String Md5Local = geraHash(new File(SistemaFileName)); //Mudar o file name 
        if (!ValidaVersao(Md5FPT, Md5Local)) {
            System.out.println("Atualizando");
            FTPDownload(SistemaFileName, DiretorioFTP);
        }

    } catch (NoSuchAlgorithmException | FileNotFoundException ex) {
        Logger.getLogger(Atualizador.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Atualizador.class.getName()).log(Level.SEVERE, null, ex);
    }
}