Guest User

Untitled

a guest
Jan 23rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. private void read() {
  2. String path = "/Users/pavel/Desktop/test/target_text.txt";
  3. try (BufferedInputStream in = new BufferedInputStream(
  4. new FileInputStream(path))) {
  5.  
  6. byte[] bytes = new byte[1024];
  7. int amountData = in.read(bytes, 0, 1024);
  8.  
  9.  
  10. while (amountData != -1 && amountData == 1024) {
  11. sb.append(new String(bytes, "UTF8"));
  12. amountData = in.read(bytes, 0, 1024);
  13. }
  14.  
  15. if (amountData != -1) {
  16. byte[] residue = new byte[amountData];
  17. System.arraycopy(bytes, 0, residue, 0, residue.length);
  18. sb.append(new String(residue, "UTF8"));
  19. }
  20.  
  21. System.out.println(sb);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26.  
  27. int amountData = in.available()%1024;
  28. for(int n=0; n<amountData; n++){
  29. in.read(bytes, 0, 1024);
  30. }
  31.  
  32. in.read(bytes, 0, in.available());
  33.  
  34. String path = "/Users/pavel/Desktop/test/target_text.txt";
  35.  
  36. StringBuilder sb = new StringBuilder();
  37. try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), Charset.forName("UTF-8")))) {
  38.  
  39. String line;
  40.  
  41. while ((line = reader.readLine()) != null) {
  42. sb.append(line + "n");
  43. }
  44.  
  45. System.out.println(sb);
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
Add Comment
Please, Sign In to add comment