Version 2The application does now write more than one line in the textfile and ends when the user types "stop".
GeSHi (java):
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
/*--------------------------------------------------------------------------
//Author List:
// deAppel <Creator>
//
//Description:
// A sample code that creates a directory, a file and saves data to the file.
// There is plenty of room for improvement but i don't have a lot of time at the moment..
// *update* the application does now write more than one line in the textfile and ends when the user types "stop".
//
//Environment:
// This software was developed using Eclipse and Java 1.6
//
//Copyright Information:
// Copyright (C) 2007 <Institution><None>
// Ark de Appel www.engineeringserver.com
//
//----------------------------------------------------------------------*/
public class CreateDirs{
public static void main
(String[] args
) {
// create directory
if (fDir.exists()){
System.
out.
println("Dir already exists!");
}
else{
System.
out.
println("Creating directory!");
fDir.mkdir();
}
// create empty file
String sDir = fDir.
toString();
File fFile =
new File(sDir +
"\\File.txt");
try {
fFile.createNewFile();
// TODO Auto-generated catch block
e.printStackTrace();
}
// type text
String s =
"";
//initialize the string.. , don't forget to initialize a variable.. System.
out.
println("Type \"stop\" to stop the application.");
System.
out.
print("Type text: ");
try {
s = br.readLine(); //reads a line at the time.. look how to read more at once.. *update* works now
// add data to file
while (!s.equals("stop")){
s = br.readLine();
if (!s.equals("stop")) // do not write stop into the textfile
bw.write(s); //write user typed data
bw.newLine();
//System.out.println("Typed: " + s);
}
// test if it writes to a file.
//bw.newLine();
//bw.write("hard coded text --++--++--");
bw.close();
System.
out.
println("Data saved in: " + sDir +
"\\File.txt");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Created by GeSHI 1.0.7.20