Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Create directory, file and write data  (Read 4659 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: August 08, 2007, 03:41:32 AM »

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. //
  17. //Environment:
  18. //   This software was developed using Eclipse and Java 1.6
  19. //
  20. //Copyright Information:
  21. //   Copyright (C) 2007      <Institution><None>
  22. // Ark de Appel www.engineeringserver.com
  23. //
  24. //----------------------------------------------------------------------*/
  25.  
  26. public class CreateDirs{
  27.  
  28. public static void main(String[] args) {
  29.  
  30. // create directory
  31. File fDir = new File("c:\\tmpfile");
  32. if (fDir.exists()){
  33. System.out.println("Dir already exists!");
  34. }
  35. else{
  36. fDir.mkdir();
  37. System.out.println("Dir created!");
  38. }
  39.  
  40. // create empty file
  41. String sDir = fDir.toString();
  42. File fFile = new File(sDir + "\\File.txt");
  43. try {
  44. fFile.createNewFile();
  45. } catch (IOException e) {
  46. // TODO Auto-generated catch block
  47. e.printStackTrace();
  48. }
  49. System.out.println(sDir);
  50.  
  51. // type text
  52.  
  53. System.out.print("Type text: ");
  54. String s = ""; //initialize the string.. , don't forget to initialize a variable..
  55. try {
  56. s = br.readLine(); //reads a line at the time.. look how to read more at once..
  57. System.out.println("Typed: " + s);
  58.  
  59. // add data to file
  60. FileWriter fw = new FileWriter(sDir + "\\File.txt");
  61.  
  62. bw.write(s); //write user typed data
  63. bw.newLine();
  64. bw.write("hard coded text --++--++--");
  65. bw.close();
  66. System.out.println("Data saved in: " + sDir + "\\File.txt");
  67. } catch (IOException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
deAppel
Favorite member
Member
*

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

Awards
« Reply #1 on: August 10, 2007, 12:39:00 AM »

Version 2

The application does now write more than one line in the textfile and ends when the user types "stop".

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
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...
AdnanAhsan
Member
*

Reputation: -1
Offline Offline
Posts: 11
Referrals: 0

Awards
« Reply #2 on: August 26, 2007, 05:19:10 AM »

Very nice admin, you are really helping me to learn Java ...
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #3 on: June 29, 2008, 03:50:56 PM »

Very nice admin, you are really helping me to learn Java ...


Glad i could help :) if you have a question about java programming let me know :)
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here