September 04, 2010, 04:18:32 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: A sample code that creates a directory, a file and saves data to the file.  (Read 1532 times)
0 Members and 1 Guest are viewing this topic.
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0

Awards
« on: October 28, 2007, 03:23:30 PM »

Code
GeSHi (java):
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8.  
  9. /*--------------------------------------------------------------------------
  10. //Author List:
  11. //   deAppel <Creator>
  12. //
  13. //Description:
  14. // A sample code that creates a directory, a file and saves data to the file.
  15. // There is plenty of room for improvement but i don't have a lot of time at the moment..
  16. // *update* the application does now write more than one line in the textfile and ends when the user types "stop".
  17. //
  18. //Environment:
  19. //   This software was developed using Eclipse and Java 1.6
  20. //
  21. //Copyright Information:
  22. //   Copyright (C) 2007      <Institution><None>
  23. // Ark de Appel www.engineeringserver.com
  24. //
  25. //----------------------------------------------------------------------*/
  26.  
  27. public class CreateDirs{
  28.  
  29. public static void main(String[] args) {
  30.  
  31. // create directory
  32. File fDir = new File("c:\\tmpfile");
  33. if (fDir.exists()){
  34. System.out.println("Dir already exists!");
  35. }
  36. else{
  37.  
  38. System.out.println("Creating directory!");
  39. fDir.mkdir();
  40. }
  41.  
  42. // create empty file
  43. String sDir = fDir.toString();
  44. File fFile = new File(sDir + "\\File.txt");
  45. try {
  46. fFile.createNewFile();
  47. } catch (IOException e) {
  48. // TODO Auto-generated catch block
  49. e.printStackTrace();
  50. }
  51. System.out.println(sDir);
  52.  
  53. // type text
  54.  
  55.  
  56. String s = ""; //initialize the string.. , don't forget to initialize a variable..
  57. System.out.println("Type \"stop\" to stop the application.");
  58. System.out.print("Type text: ");
  59. try {
  60. s = br.readLine(); //reads a line at the time.. look how to read more at once.. *update* works now
  61.  
  62. // add data to file
  63. FileWriter fw = new FileWriter(sDir + "\\File.txt");
  64.  
  65. while (!s.equals("stop")){
  66. s  = br.readLine();
  67. if (!s.equals("stop")) // do not write stop into the textfile
  68. bw.write(s); //write user typed data
  69. bw.newLine();
  70.  
  71. //System.out.println("Typed: " + s);
  72. }
  73.  
  74. // test if it writes to a file.
  75. //bw.newLine();
  76. //bw.write("hard coded text --++--++--");
  77.  
  78. bw.close();
  79. System.out.println("Data saved in: " + sDir + "\\File.txt");
  80. } catch (IOException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. }
  85. }
Created by GeSHI 1.0.7.20

As usual, comments/suggestions etc can be posted here :)
Logged

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
Javaforums.net :: a community about Java software development.
« on: October 28, 2007, 03:23:30 PM »

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.237 seconds with 32 queries.

Google visited last this page August 30, 2010, 01:56:09 AM