public void copyHistorial() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); File desktop = new File(System.getProperty("user.home"), "Desktop"); fileChooser.setCurrentDirectory(desktop); if (fileChooser.showOpenDialog(window) == JFileChooser.APPROVE_OPTION) { String path = fileChooser.getSelectedFile().getAbsolutePath(); Date date = new Date(System.currentTimeMillis()); SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); String dateText = df.format(date); String name = "/historial(" + dateText + ").txt"; PrintWriter writer = null; try { writer = new PrintWriter(path + name, "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < actionList.size(); i++) { writer.println(actionList.get(i).toString()); } writer.close(); } else { JOptionPane.showMessageDialog( null, "Alert", "Something has gone wrong", JOptionPane.ERROR_MESSAGE); } }
static void writeMovesToFile() { PrintWriter moveWriter = null; try { moveWriter = new PrintWriter("games/" + getCurrentTime() + ".moves", "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException exception) { exception.printStackTrace(); } List<String> firstPlayerMoves = null; List<String> secondPlayerMoves = null; if (humanAsWhite) { firstPlayerMoves = humanMoves; secondPlayerMoves = computerMoves; } else { firstPlayerMoves = computerMoves; secondPlayerMoves = humanMoves; } for (int i = 0; i < firstPlayerMoves.size(); i++) { moveWriter.print((i + 1) + " " + firstPlayerMoves.get(i) + " "); if (i <= secondPlayerMoves.size() - 1) { moveWriter.print(secondPlayerMoves.get(i)); } moveWriter.println(); } moveWriter.close(); }
public static void saveTxtModel(String filename) { // create file PrintWriter writer; try { writer = new PrintWriter(filename, "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); return; } writer.print(Integer.toString(words)); writer.print(" "); writer.print(Integer.toString(size)); writer.print("\n"); // loop through all words Iterator<Entry<String, float[]>> it = WordMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, float[]> pairs = (Map.Entry<String, float[]>) it.next(); String key = pairs.getKey(); float[] value = pairs.getValue(); writer.print(key + " "); writer.print(getVectorAsString(value) + "\n"); } writer.close(); }
public void writeFile(String str) { PrintWriter writer; try { writer = new PrintWriter("D:/outputTextFile.txt", "UTF-8"); writer.println(str); writer.close(); } catch (FileNotFoundException | UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
private void clearSaved() { PrintWriter writer = null; try { writer = new PrintWriter("config.txt", "UTF-8"); writer.println("PhoneNumbers:"); writer.println("Items:"); results.setText("Config.txt has been cleared."); } catch (FileNotFoundException | UnsupportedEncodingException e1) { e1.printStackTrace(); } writer.close(); }
// helper for debugging @SuppressWarnings("unused") protected static void printGenerateTrees(DTree[] trees) { for (DTree dtree : trees) if (dtree != null) { try { PrintWriter writer = new PrintWriter("/tmp/h2o-3.tree" + ++counter + ".txt", "UTF-8"); writer.println(dtree.root().toString2(new StringBuilder(), 0)); writer.close(); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println(dtree.root().toString2(new StringBuilder(), 0)); } }
public static void main(String[] args) { String fileName = ""; // generate txt file try { generatePeople(100); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } fileName = "listOfPeople.txt"; List<String> allLines; // defaults to project's path try { allLines = Files.readAllLines(Paths.get(fileName)); } catch (IOException e) { allLines = new ArrayList<String>(); e.printStackTrace(); } // Parses the data set from text file to Person List ArrayList<Person> people = new ArrayList<Person>(); for (String line : allLines) { String[] rawPerson = line.split("\\s+"); String[] rawYears = rawPerson[1].split("-"); people.add( new Person(rawPerson[0], Integer.parseInt(rawYears[0]), Integer.parseInt(rawYears[1]))); } // Run the calculations Census census = new Census(people); int year = census.startCensus(1900, 2000); int population = census.getPopulation(year); // Result System.out.println("Biggest population was " + population + " in " + year); PrintWriter writer; try { writer = new PrintWriter("censusResults.txt", "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException e) { writer = null; e.printStackTrace(); } if (writer != null) { writer.println("Biggest population was " + population + " in " + year); for (String line : census.getAllYears().split("\n")) { writer.println(line); } } writer.close(); }
/** * @param list: list of tuples to be written into file * @param query: helps at creating the file name * @return file */ public static File writeDiscriminatoryWordsToFile(List<WordFrequencyTuple> list, String query) { if (list.isEmpty()) { System.out.println(" No discriminatory words for:" + query); return new File(""); } try { PrintWriter writer = new PrintWriter(getFileName(query), CSN); for (WordFrequencyTuple wordFrequencyTuple : list) { writer.println(wordFrequencyTuple.getWord() + " " + wordFrequencyTuple.getFrequency()); } writer.close(); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); } return new File(getFileName(query)); }
/** * @param searchResults: list of search results to be written into file * @param query: helps at creating the file name * @return file */ public static File writeVideoInfoToFile(List<SearchResult> searchResults, String query) { if (searchResults.isEmpty()) { System.out.println(" No video info to write to file for query:" + query); return new File(""); } try { PrintWriter writer = new PrintWriter(getFileName(query), CSN); for (SearchResult videoResult : searchResults) { writer.println(videoResult.getSnippet().getTitle()); writer.println(videoResult.getSnippet().getDescription()); } writer.close(); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); } return new File(getFileName(query)); }
public static void createSyntacticVec(String path, String pathLemmaMap, String filename) { // create file PrintWriter writer; try { writer = new PrintWriter(filename, "UTF-8"); } catch (FileNotFoundException | UnsupportedEncodingException e) { e.printStackTrace(); return; } HashMap<String, float[]> leadingLemma = new HashMap<String, float[]>(); DataInputStream dis = null; BufferedInputStream bis = null; BufferedReader br = null; double len = 0; float vector = 0; try { bis = new BufferedInputStream(new FileInputStream(path)); dis = new DataInputStream(bis); br = new BufferedReader(new FileReader(pathLemmaMap)); words = Integer.parseInt(readString(dis)); size = Integer.parseInt(readString(dis)); String key; float[] value = null; for (int i = 0; i < words; i++) { key = readString(dis); value = new float[size]; len = 0; for (int j = 0; j < size; j++) { vector = readFloat(dis); len += vector * vector; value[j] = vector; } len = Math.sqrt(len); String line = br.readLine(); String keyLemma = line.split("\t")[1]; if (keyLemma.equals("<unknown>")) continue; keyLemma = line.split("\t")[0]; if (leadingLemma.containsKey(keyLemma)) { float[] diff = leadingLemma.get(keyLemma); for (int j = 0; j < size; j++) { diff[j] -= value[j]; } writer.print(normalizeLemma(key) + " "); writer.print(getVectorAsString(diff) + "\n"); } else { leadingLemma.put(keyLemma, value); } } writer.close(); bis.close(); dis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }