List of usage examples for java.io FileReader FileReader
public FileReader(FileDescriptor fd)
From source file:csv.parser.CSVParser.java
@SuppressWarnings("resource") public static void main(String[] args) throws Exception { String file_to_parse;//www. ja v a 2s.c o m String[] val_array; file_to_parse = "./input/E-library-data-3.csv"; //Build reader instance //Read CSV file CSVReader reader = new CSVReader(new FileReader(file_to_parse), ';', '"', 1); //Read all rows at once List<String[]> allRows = reader.readAll(); // Read CSV line by line and use the string array as you want for (String[] row : allRows) { for (int i = 0; i < row.length; i++) { //Removing all newlines, tabs and '&' characters(invalid XML character) row[i] = row[i].replaceAll("(\\r|\\n|\\r\\n)+", " "); row[i] = row[i].replaceAll(System.getProperty("line.separator"), "; "); row[i] = row[i].replaceAll("&", "and"); } System.out.println(Arrays.toString(row)); } //Get the input fields List<String[]> map = getMap(); String[] field; //Numbering for folders, folderNum is incremented for each new file long folderNum; folderNum = 0; for (String[] row : allRows) { //Creating new folder File file1 = new File("./output//newdir//folder" + folderNum + ""); file1.mkdirs(); //Creating content file PrintWriter writer_content = new PrintWriter("./output//newdir//folder" + folderNum + "//contents", "UTF-16"); //Creating metadata_lrmi.xml PrintWriter writer_lrmi = new PrintWriter( "./output//newdir//folder" + folderNum + "//metadata_lrmi.xml", "UTF-16"); //Creating content.xml PrintWriter writer = new PrintWriter("./output//newdir//folder" + folderNum + "//content.xml", "UTF-16"); writer.println("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"no\"?>"); writer.println("<dublin_core schema=\"dc\">"); writer_lrmi.println("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"no\"?>"); writer_lrmi.println("<dublin_core shema=\"lrmi\">"); for (int i = 0; i < row.length; i++) { //After snooping data, we have to change these setting for each new csv file, as the data fileds are many times mismatched //These if-else statements take care of mismatched steps. if (i == 43) { continue; } else if (i == 43) { field = map.get(42); } else if (i == 44) { field = map.get(43); } else if (i == 45 || i == 46) { continue; } else { field = map.get(i); } //Separate multiple values val_array = parseVal(row[i]); // if (val_array.length == 0) { // continue; // } PrintWriter useWriter = writer; if (field[0].equals("lrmi")) { useWriter = writer_lrmi; } switch (field.length) { case 2: writeXML(useWriter, field[1], "", val_array); break; case 3: writeXML(useWriter, field[1], field[2], val_array); break; default: } } folderNum++; writer.println("</dublin_core>"); writer_lrmi.println("</dublin_core>"); writer.close(); writer_lrmi.close(); writer_content.close(); } }
From source file:Main.java
public final static void main(String[] args) throws Exception { final ArrayList<String> list = new ArrayList<String>(); ParserDelegator parserDelegator = new ParserDelegator(); ParserCallback parserCallback = new ParserCallback() { public void handleText(final char[] data, final int pos) { }/* ww w. ja va 2s. co m*/ public void handleStartTag(Tag tag, MutableAttributeSet attribute, int pos) { if (tag == Tag.A) { String address = (String) attribute.getAttribute(Attribute.HREF); list.add(address); } } public void handleEndTag(Tag t, final int pos) { } public void handleSimpleTag(Tag t, MutableAttributeSet a, final int pos) { } public void handleComment(final char[] data, final int pos) { } public void handleError(final java.lang.String errMsg, final int pos) { } }; parserDelegator.parse(new FileReader("a.html"), parserCallback, false); System.out.println(list); }
From source file:de.mirkosertic.invertedindex.core.FullIndexRun.java
public static void main(String[] args) throws IOException { InvertedIndex theIndex = new InvertedIndex(); UpdateIndexHandler theIndexHandler = new UpdateIndexHandler(theIndex); Tokenizer theTokenizer = new Tokenizer(new ToLowercaseTokenHandler(theIndexHandler)); File theOrigin = new File("/home/sertic/ownCloud/Textcontent"); for (File theFile : theOrigin.listFiles()) { System.out.println("Indexing " + theFile); String theFileContent = IOUtils.toString(new FileReader(theFile)); theTokenizer.process(new Document(theFile.getName(), theFileContent)); }//from ww w .j a v a2 s. com System.out.println(theIndex.getTokenCount() + " unique postings"); System.out.println(theIndex.getDocumentCount() + " documents"); theIndex.postings.entrySet().stream() .sorted((o1, o2) -> ((Integer) o1.getValue().getOccoursInDocuments().size()) .compareTo(o2.getValue().getOccoursInDocuments().size())) .forEach(t -> { System.out.println(t.getKey() + " -> " + t.getValue().getOccoursInDocuments().size()); }); System.out.println("Query"); Result theResult = theIndex.query(new TokenSequenceQuery(new String[] { "introduction", "to", "aop" })); System.out.println(theResult.getSize()); for (int i = 0; i < theResult.getSize(); i++) { System.out.println(theResult.getDoc(i).getName()); System.out.println(theIndex.rebuildContentFor(theResult.getDoc(i))); } long theCount = 100000; long theStart = System.currentTimeMillis(); for (int i = 0; i < theCount; i++) { theResult = theIndex.query(new TokenSequenceQuery(new String[] { "introduction", "to", "aop" })); } double theDuration = System.currentTimeMillis() - theStart; System.out.println(theCount + " Queries took " + theDuration + "ms"); System.out.println(theDuration / theCount); while (true) { theResult = theIndex.query(new TokenSequenceQuery(new String[] { "introduction", "to", "aop" })); } }
From source file:edu.usc.ee599.CommunityStats.java
public static void main(String[] args) throws Exception { File dir = new File("results5"); PrintWriter writer = new PrintWriter(new FileWriter("results5_stats.txt")); File[] files = dir.listFiles(); DescriptiveStatistics statistics1 = new DescriptiveStatistics(); DescriptiveStatistics statistics2 = new DescriptiveStatistics(); for (File file : files) { BufferedReader reader = new BufferedReader(new FileReader(file)); String line1 = reader.readLine(); String line2 = reader.readLine(); int balanced = Integer.parseInt(line1.split(",")[1]); int unbalanced = Integer.parseInt(line2.split(",")[1]); double bp = (double) balanced / (double) (balanced + unbalanced); double up = (double) unbalanced / (double) (balanced + unbalanced); statistics1.addValue(bp);/*from w ww .ja v a 2 s . co m*/ statistics2.addValue(up); } writer.println("AVG Balanced %: " + statistics1.getMean()); writer.println("AVG Unbalanced %: " + statistics2.getMean()); writer.println("STD Balanced %: " + statistics1.getStandardDeviation()); writer.println("STD Unbalanced %: " + statistics2.getStandardDeviation()); writer.flush(); writer.close(); }
From source file:akori.Impact.java
static public void main(String[] args) throws IOException { String PATH = "E:\\Trabajos\\AKORI\\datosmatrizgino\\"; String PATHIMG = "E:\\NetBeansProjects\\AKORI\\Proccess_1\\ImagesPages\\"; for (int i = 1; i <= 32; ++i) { for (int k = 1; k <= 15; ++k) { System.out.println("Matrix " + i + "-" + k); BufferedImage img = null; try { img = ImageIO.read(new File(PATHIMG + i + ".png")); } catch (IOException ex) { ex.getStackTrace();// w ww. java2 s . c o m } int ymax = img.getHeight(); int xmax = img.getWidth(); double[][] imagen = new double[ymax][xmax]; BufferedReader in = null; try { in = new BufferedReader(new FileReader(PATH + i + "-" + k + ".txt")); } catch (FileNotFoundException ex) { ex.getStackTrace(); } String linea; ArrayList<String> lista = new ArrayList<String>(); HashMap<String, String> lista1 = new HashMap<String, String>(); try { for (int j = 0; (linea = in.readLine()) != null; ++j) { String[] datos = linea.split(","); int x = (int) Double.parseDouble(datos[1]); int y = (int) Double.parseDouble(datos[2]); if (x >= xmax || y >= ymax || x <= 0 || y <= 0) { continue; } lista.add(x + "," + y); } } catch (Exception ex) { ex.getStackTrace(); } try { in.close(); } catch (IOException ex) { ex.getStackTrace(); } Iterator iter = lista.iterator(); int[][] matrix = new int[lista.size()][2]; for (int j = 0; iter.hasNext(); ++j) { String xy = (String) iter.next(); String[] datos = xy.split(","); matrix[j][0] = Integer.parseInt(datos[0]); matrix[j][1] = Integer.parseInt(datos[1]); } for (int j = 0; j < matrix.length; ++j) { int std = 50; int x = matrix[j][0]; int y = matrix[j][1]; imagen[y][x] += 1; double aux; normalMatrix(imagen, y, x, std); } FileWriter fw = new FileWriter(PATH + "Matrix" + i + "-" + k + ".txt"); BufferedWriter bw = new BufferedWriter(fw); for (int j = 0; j < imagen.length; ++j) { for (int t = 0; t < imagen[j].length; ++t) { if (t + 1 == imagen[j].length) bw.write(imagen[j][t] + ""); else bw.write(imagen[j][t] + ","); } bw.write("\n"); } bw.close(); } } }
From source file:PCC.java
/** * @param args the command line arguments * @throws java.io.IOException//from ww w. j ava 2 s . c o m */ public static void main(String[] args) throws IOException { // TODO code application logic here PearsonsCorrelation corel = new PearsonsCorrelation(); PCC method = new PCC(); ArrayList<String> name = new ArrayList<>(); Multimap<String, String> genes = ArrayListMultimap.create(); BufferedWriter bw = new BufferedWriter(new FileWriter(args[1])); BufferedReader br = new BufferedReader(new FileReader(args[0])); String str; while ((str = br.readLine()) != null) { String[] a = str.split("\t"); name.add(a[0]); for (int i = 1; i < a.length; i++) { genes.put(a[0], a[i]); } } for (String key : genes.keySet()) { double[] first = new double[genes.get(key).size()]; int element1 = 0; for (String value : genes.get(key)) { double d = Double.parseDouble(value); first[element1] = d; element1++; } for (String key1 : genes.keySet()) { if (!key.equals(key1)) { double[] second = new double[genes.get(key1).size()]; int element2 = 0; for (String value : genes.get(key1)) { double d = Double.parseDouble(value); second[element2] = d; element2++; } double corrlation = corel.correlation(first, second); if (corrlation > 0.5) { bw.write(key + "\t" + key1 + "\t" + corrlation + "\t" + method.pvalue(corrlation, second.length) + "\n"); } } } } br.close(); bw.close(); }
From source file:ZipCompress.java
public static void main(String[] args) throws IOException { FileOutputStream f = new FileOutputStream("test.zip"); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("A test of Java Zipping"); // No corresponding getComment(), though. for (int i = 0; i < args.length; i++) { System.out.println("Writing file " + args[i]); BufferedReader in = new BufferedReader(new FileReader(args[i])); zos.putNextEntry(new ZipEntry(args[i])); int c;/* ww w . ja va 2 s .c o m*/ while ((c = in.read()) != -1) out.write(c); in.close(); } out.close(); // Checksum valid only after the file has been closed! System.out.println("Checksum: " + csum.getChecksum().getValue()); // Now extract the files: System.out.println("Reading file"); FileInputStream fi = new FileInputStream("test.zip"); CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32()); ZipInputStream in2 = new ZipInputStream(csumi); BufferedInputStream bis = new BufferedInputStream(in2); ZipEntry ze; while ((ze = in2.getNextEntry()) != null) { System.out.println("Reading file " + ze); int x; while ((x = bis.read()) != -1) System.out.write(x); } System.out.println("Checksum: " + csumi.getChecksum().getValue()); bis.close(); // Alternative way to open and read zip files: ZipFile zf = new ZipFile("test.zip"); Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry) e.nextElement(); System.out.println("File: " + ze2); // ... and extract the data as before } }
From source file:org.hcmut.emr.SessionBuilder.java
public static void main(String[] args) throws FileNotFoundException, IOException { try (BufferedReader br = new BufferedReader( new FileReader("/home/sinhlk/myspace/emr/src/main/resources/patern"))) { ObjectMapper jsonMapper = new ObjectMapper(); String line = br.readLine(); Map<String, String> result = new HashMap<String, String>(); List<NameValuePair> list = new ArrayList<>(); while (line != null) { if (line != null && line != "") { list.add(new BasicNameValuePair(line.trim().toLowerCase(), SessionBuilder.buildValue(line))); result.put(line.trim().toLowerCase(), SessionBuilder.buildValue(line)); line = br.readLine();/*from w ww . j a va 2 s . co m*/ } } System.out.println(jsonMapper.writeValueAsString(list)); File file = new File("/home/sinhlk/myspace/emr/src/main/resources/session.js"); jsonMapper.writeValue(file, list); } }
From source file:my.yelp.populate.java
public static void main(String[] args) throws FileNotFoundException, ParseException, IOException, java.text.ParseException { try {/*from ww w . j a v a 2s.com*/ DbConnection A1 = new DbConnection(); Connection con = A1.getConnection(); JSONParser jsonParser; jsonParser = new JSONParser(); Object obj1 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_user.json")); Object obj2 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_business.json")); Object obj3 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_review.json")); Object obj4 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_checkin.json")); JSONArray jsonArray1; jsonArray1 = (JSONArray) obj1; JSONArray jsonArray2; jsonArray2 = (JSONArray) obj2; JSONArray jsonArray3; jsonArray3 = (JSONArray) obj3; JSONArray jsonArray4; jsonArray4 = (JSONArray) obj4; // yelp_user String yelping_since, name1, user_id, type1; Long review_count1, fans; Double average_stars; Statement stmt; stmt = con.createStatement(); stmt.executeUpdate("Delete from N_User"); for (int i = 0; i < (jsonArray1.size()); i++) { JSONObject jsonObject = (JSONObject) jsonArray1.get(i); yelping_since = (String) jsonObject.get("yelping_since") + "-01"; JSONArray friends = (JSONArray) jsonObject.get("friends"); int friends_size = friends.size(); review_count1 = (Long) jsonObject.get("review_count"); name1 = (String) jsonObject.get("name"); user_id = (String) jsonObject.get("user_id"); fans = (Long) jsonObject.get("fans"); average_stars = (Double) jsonObject.get("average_stars"); type1 = (String) jsonObject.get("type"); try (PreparedStatement pstmt1 = con.prepareStatement( "Insert INTO N_User(yelping_since,friends_size,review_count,name,user_id,fans,average_stars,type) VALUES(?,?,?,?,?,?,?,?)")) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date myDate = format.parse(yelping_since); pstmt1.setDate(1, new java.sql.Date(myDate.getTime())); pstmt1.setInt(2, friends_size); pstmt1.setLong(3, review_count1); pstmt1.setString(4, name1); pstmt1.setString(5, user_id); pstmt1.setLong(6, fans); pstmt1.setDouble(7, average_stars); pstmt1.setString(8, type1); pstmt1.executeUpdate(); } catch (java.text.ParseException ex) { Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex); } } //yelp_business String business_id, address, city, state, name, type_business; Double stars; for (int i = 0; i < jsonArray2.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray2.get(i); business_id = (String) jsonObject.get("business_id"); address = (String) jsonObject.get("full_address"); city = (String) jsonObject.get("city"); state = (String) jsonObject.get("state"); name = (String) jsonObject.get("name"); stars = (Double) jsonObject.get("stars"); type_business = (String) jsonObject.get("type"); try (PreparedStatement pstmt2 = con.prepareStatement( "Insert INTO N_Business(business_id,address,city,state,name,stars,type_business) VALUES(?,?,?,?,?,?,?)")) { pstmt2.setString(1, business_id); pstmt2.setString(2, address); pstmt2.setString(3, city); pstmt2.setString(4, state); pstmt2.setString(5, name); pstmt2.setDouble(6, stars); pstmt2.setString(7, type_business); pstmt2.executeUpdate(); pstmt2.close(); } } //Category Table String[] categories = { "Active Life", "Arts & Entertainment", "Automotive", "Car Rental", "Cafes", "Beauty & Spas", "Convenience Stores", "Dentists", "Doctors", "Drugstores", "Department Stores", "Education", "Event Planning & Services", "Flowers & Gifts", "Food", "Health & Medical", "Home Services", "Home & Garden", "Hospitals", "Hotels & travel", "Hardware stores", "Grocery", "Medical Centers", "Nurseries & Gardening", "Nightlife", "Restaurants", "Shopping", "Transportation" }; JSONArray category; String[] individual_category = new String[100]; int count = 0, flag = 0, m = 0, n = 0; String[] business_category = new String[50]; String[] subcategory = new String[50]; for (int i = 0; i < jsonArray2.size(); i++) { JSONObject jsonObject3 = (JSONObject) jsonArray2.get(i); String business_id2 = (String) jsonObject3.get("business_id"); category = (JSONArray) jsonObject3.get("categories"); for (int j = 0; j < category.size(); j++) { individual_category[j] = (String) category.get(j); count = count + 1; } for (int k = 0; k < count; k++) { for (String categorie : categories) { if (individual_category[k].equals(categorie)) { flag = 1; break; } } if (flag == 1) { business_category[m] = individual_category[k]; m = m + 1; flag = 0; } else { subcategory[n] = individual_category[k]; n = n + 1; } } for (int p = 0; p < m; p++) { for (int q = 0; q < n; q++) { try (PreparedStatement pstmt3 = con.prepareStatement( "INSERT INTO N_Category(business_id,category,subcategory) VALUES(?,?,?)")) { pstmt3.setString(1, business_id2); pstmt3.setString(2, business_category[p]); pstmt3.setString(3, subcategory[q]); pstmt3.executeUpdate(); } } } count = 0; m = 0; n = 0; } //yelp_review String user_id3, review_id, type3, business_id3, text, text1, review_date; Long stars3; int votes = 0; Integer no_votes; JSONObject votes_info; Set<String> keys; for (int i = 0; i < jsonArray3.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray3.get(i); votes_info = (JSONObject) jsonObject.get("votes"); keys = votes_info.keySet(); for (String r_key : keys) { votes = (int) (votes + (Long) votes_info.get(r_key)); } no_votes = toIntExact(votes); user_id3 = (String) jsonObject.get("user_id"); review_id = (String) jsonObject.get("review_id"); business_id3 = (String) jsonObject.get("business_id"); review_date = (String) jsonObject.get("date"); text1 = (String) jsonObject.get("text"); text = text1.substring(0, Math.min(1000, text1.length())); stars3 = (Long) jsonObject.get("stars"); type3 = (String) jsonObject.get("type"); try (PreparedStatement pstmt4 = con.prepareStatement( "Insert INTO N_Review(no_votes,user_id,review_id,business_id,review_date,text,stars,type) VALUES(?,?,?,?,?,?,?,?)")) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date myDate = format.parse(review_date); pstmt4.setInt(1, no_votes); pstmt4.setString(2, user_id3); pstmt4.setString(3, review_id); pstmt4.setString(4, business_id3); pstmt4.setDate(5, new java.sql.Date(myDate.getTime())); pstmt4.setString(6, text); pstmt4.setLong(7, stars3); pstmt4.setString(8, type3); pstmt4.executeUpdate(); pstmt4.close(); } } //Checkin_Info JSONObject checkin_info; String business_id4; Long check_in_count; Set<String> keys1; String[] timing = new String[10]; int n1 = 0, time, hour; //Inserting into checkin_info for (int i = 0; i < jsonArray4.size(); i++) { JSONObject jsonObject4 = (JSONObject) jsonArray4.get(i); checkin_info = (JSONObject) jsonObject4.get("checkin_info"); business_id4 = (String) jsonObject4.get("business_id"); keys1 = checkin_info.keySet(); for (String key : keys1) { check_in_count = (Long) checkin_info.get(key); for (String x : key.split("-")) { timing[n1] = x; n1 = n1 + 1; } n1 = 0; hour = Integer.parseInt(timing[0]); time = Integer.parseInt(timing[1]); try (PreparedStatement pstmt5 = con.prepareStatement( "INSERT INTO check_info(business_id,hour,day,check_in_count)VALUES(?,?,?,?)")) { pstmt5.setString(1, business_id4); pstmt5.setInt(2, hour); pstmt5.setInt(3, time); pstmt5.setLong(4, check_in_count); pstmt5.executeUpdate(); } } } con.close(); } catch (SQLException ex) { Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ReplaceReader.java
public static void main(String[] args) { try {/*from w w w . j av a 2s. co m*/ UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JFrame f = new JFrame("JEditorPane with Custom Reader"); JEditorPane ep = new JEditorPane(); f.getContentPane().add(new JScrollPane(ep)); f.setSize(400, 300); f.setVisible(true); HTMLEditorKit kit = new HTMLEditorKit() { public Document createDefaultDocument() { HTMLDocument doc = new CustomHTMLDocument(getStyleSheet()); doc.setAsynchronousLoadPriority(4); doc.setTokenThreshold(100); return doc; } }; ep.setEditorKit(kit); try { Document doc = ep.getDocument(); doc.putProperty("IgnoreCharsetDirective", new Boolean(true)); kit.read(new FileReader(args[0]), doc, 0); } catch (Exception e) { System.out.println("Exception while reading HTML " + e); } }