Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package javaapplication1;
  7.  
  8. import java.io.*;
  9. import java.util.ArrayList;
  10.  
  11.  
  12.  
  13. /**
  14. *
  15. * @author grahg
  16. */
  17. public class JavaApplication1 {
  18. public static void array_to_csv() throws IOException
  19. {
  20.  
  21.  
  22. // PRVNI
  23. int velX = 10;
  24. int velY = 10;
  25. double [][]matice = new double[velX][velY];
  26. String csv = velX + "," + velY + ",\n";
  27.  
  28. for (int i=0; i<velX; ++i) {
  29. for (int j=0; j<velY; ++j) {
  30. matice[i][j]= (double)Math.random() + (double)Math.random() * 1000;
  31. csv = csv + matice[i][j] + ",";
  32. }
  33. csv = csv + "\n";
  34. }
  35.  
  36. File file = new File("/home/grahg/a.csv");
  37. if (!file.exists())
  38. file.createNewFile();
  39.  
  40. else if (file.canRead()) {
  41. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  42. BufferedWriter bw = new BufferedWriter(fw);
  43. bw.write(csv);
  44. bw.close();
  45. }
  46. }
  47.  
  48. public static void print_csv() throws IOException
  49. {
  50.  
  51. // DRUHY
  52. BufferedReader br = new BufferedReader(new FileReader("/home/grahg/a.csv"));
  53. String line;
  54.  
  55. line = br.readLine();
  56. String[] parts = line.split(",");
  57. int x = Integer.parseInt(parts[0]);
  58. int y = Integer.parseInt(parts[1]);
  59.  
  60. double [][]matice2 = new double[x][y];
  61.  
  62. for (int i=0; i<y; ++i) {
  63. if ( (line = br.readLine() ) == null) break;
  64. String[] values = line.split(",");
  65.  
  66. for (int j=0; j<y; ++j) {
  67. matice2[i][j]= Float.parseFloat(values[j]);
  68. System.out.print(matice2[i][j] + ",");
  69. }
  70. System.out.println();
  71. }
  72.  
  73. br.close();
  74. }
  75. /**
  76. * @param args the command line arguments
  77. */
  78. public static void main(String[] args) throws IOException {
  79. array_to_csv();
  80. //print_csv();
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement