Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: kourkour on Apr 28th, 2012  |  syntax: Java  |  size: 2.33 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.*;
  2. public class IO_Tester {
  3.  
  4. public static void main() {
  5. System.out.println("Dwste enan akeraio:");
  6. int i = readInt();
  7. System.out.println("Dwste enan float:");
  8. float f = readFloat();
  9. System.out.println("Dwste ena string:");
  10. String s = readString();
  11. System.out.println("Dwste mia boolean:");
  12. boolean b = readBoolean();
  13. System.out.println("i="+i+"\tf="+f+"\ts="+s+"\tb="+b);
  14. try {
  15. FileOutputStream fil = new FileOutputStream(s);
  16. BufferedOutputStream buf = new BufferedOutputStream(fil);
  17. DataOutputStream dat = new DataOutputStream(buf);
  18. dat.writeInt(i);
  19. dat.writeFloat(f);
  20. dat.writeBoolean(b);
  21. dat.close();
  22. } catch (FileNotFoundException e) {}
  23. catch (IOException e) {}
  24.  
  25.  
  26. try {
  27. FileInputStream fil = new FileInputStream(s);
  28. BufferedInputStream buf = new BufferedInputStream(fil);
  29. DataInputStream dat = new DataInputStream(buf);
  30. i+=dat.readInt();
  31. f*=dat.readFloat();
  32. b=!dat.readBoolean();
  33. dat.close();
  34. } catch (FileNotFoundException e) {}
  35. catch (IOException e) {}
  36. System.out.println("i="+i+"\tf="+f+"\ts="+s+"\tb="+b);
  37. }
  38.  
  39. public static int readInt() {
  40. byte b[] = new byte[16];
  41. String str;
  42. try {
  43. System.in.read(b);
  44. str = (new String(b)).trim();
  45. return Integer.parseInt(str);
  46. } catch (IOException e) {
  47. System.out.println("Exception: " + e.toString());
  48. return 0;
  49. } catch (NumberFormatException e) {
  50. System.out.println("Exception: " + e.toString() + "\n Returned value: -1");
  51. return -1;
  52. }
  53. }
  54.  
  55. public static float readFloat() {
  56. byte b[] = new byte[16];
  57. String str;
  58. try {
  59. System.in.read(b);
  60. str = (new String(b)).trim();
  61. return Float.parseFloat(str);
  62. } catch (IOException e) {
  63. System.out.println("Exception: " + e.toString());
  64. return 0;
  65. }catch (NumberFormatException e){
  66. System.out.println("Exception: " + e.toString() + "\n Returned value: -1");
  67. return -1;
  68. }
  69. }
  70.  
  71. public static String readString() {
  72. byte b[] = new byte[32];
  73. String str;
  74. try {
  75. System.in.read(b);
  76. str = (new String(b)).trim();
  77. return str;
  78. } catch (IOException e) {
  79. System.out.println("Exception: " + e.toString());
  80. return "";
  81. }
  82. }
  83.  
  84. public static boolean readBoolean() {
  85. byte b[] = new byte[16];
  86. String str;
  87. try {
  88. System.in.read(b);
  89. str = (new String(b)).trim();
  90. Boolean bool = new Boolean(str);
  91. return bool.booleanValue();
  92. } catch (IOException e) {
  93. System.out.println("Exception: " + e.toString());
  94. return false;
  95. }
  96. }
  97.  
  98. }