Jan 06

What is the output of the following code?

import java.io.*;

class Dog{

public static void main(String args[]) {

File folder = new File(“test_folder”);
folder.mkdir();

File file = new File(folder, “Test.txt”);
try {

file.createNewFile();

}catch (IOException e) { }

for (int i = 1; i<=3; i++){

System.out.println(“loop  “+i);
File newFileName = new File(folder, “new_name.txt”);
System.out.println(“rename of file:  “+file.renameTo(newFileName));
File newFolderName= new File (“new_test_folder”);
System.out.println(“rename of folder:  “+folder.renameTo(newFolderName));

}

}

}

Antwort/ answer

Tagged with:
Jan 05

Which of these lines of code are not compiling correctly?

BufferedWriter a = new BufferedWriter(new File(“Test1.txt”)); //1
BufferedWriter b = new BufferedWriter(new FileWriter(“Test2.txt”));
//2
BufferedWriter c = new BufferedWriter(new PrintWriter(“Test3.txt”));
//3
BufferedWriter d = new BufferedWriter(new BufferedWriter(c));
//4

Antwort/ answer
Tagged with:
Jan 04

Let’s take once more our standard example to show listing of files and folders:

File folder= new File(“test_folder”);
folder.mkdir();
// creation of folder

File file = new File(folder, “Test.txt”);
try {
file.createNewFile();
}catch (IOException e) { }
// creation of file

To list files and folders we have to act in the following way:

File search = new File(“test_folder”); // instruction where to search: here folder test_folder
String[ ] find = new String [100]; // Creation of a string-array to save the search-results
find = search.list();

for (String x : find) // Output of the found sub-folders and files
System.out.println(x);

Tagged with:
Jan 03

During program-execution it can also be checked weather we are dealing with a file or a folder. Let’s take the example of the last days again:

File folder= new File(“test_folder”);
folder.mkdir();
// creation of folder

File file = new File(folder, “Test.txt”);
try {
file.createNewFile();
}catch (IOException e) { }
// creation of file

check looks like this:

System.out.println(file.isFile()); // true
System.out.println(file.isDirectory()); // false
System.out.println(folder.isFile()); // false
System.out.println(folder.isDirectory()); // true

Tagged with:
Jan 02

Today we are talking about deletion of files and folders. To have a better overview first of all the already known definitions:

File folder= new File(“test_folder”);
folder.mkdir();
// creation of folder

File file = new File(folder, “Test.txt”);
try {
file.createNewFile();
}catch (IOException e) { }
// creation of file

To delete the files and folders:

System.out.println(folder.delete()); //false; there is still a file in the folder; folder is not deleted

System.out.println(file.delete()); // true; deletion of file
System.out.println(folder.delete());
// true;  folder is now empty


Tagged with:
Dec 30

Today I would like to explain how to rename files and folders:

First of all the already known definitions for creation of files and folders:

File folder= new File(“test_folder”);
folder.mkdir();

File file = new File(folder, “Test.txt”);
try {
file.createNewFile();
}catch (IOException e) { }

For renaming we have first of all to create a new Object from type file:

File newFileName= new File(folder, “new_name.txt”); // respectively
File newFolderName= new File (“new_test_folder”);

now we can do renaming:

file.renameTo(newFileName);
folder.renameTo(newFolderName);

In case you are using the statements above with a already existing file/folder-name nothing will happen and only false returned. So watch out for such questions in SCJP!

Tagged with:
Dec 28

We know already how to create a file, but how to create a folder?

File folder = new File(“test_folder”); // Name of folder is created, but folder itself not
folder .mkdir(); // creation of folder

File file= new File(
folder , “Test.txt”); // filename is created, but file itself not
try { // as already known creation of file need to be put in try/catch-block
file.createNewFile(); // creation of file in a predefined folder
}catch (IOException e) { }

Tagged with:
Dec 26

PrintWriter’s functions are similar to FileWriter, but with some additional methods:

PrintWriter write_file= new PrintWriter(file); // Creation of a new object of type PrintWriter
write_file.write(“This is the first line\n…and this the second”); // Methods of FileWriter are working here as well
write_file.println(“…now we are adding something to the second line”); // additionally we can write in file with print/println
write_file.print(“now we are really adding the third line”);
write_file.flush();
write_file.close();


Tagged with:
Dec 24

analogue to BufferedWriter BufferedReader wrapps also – this time FileReader.

Just to compare; for use of FileReader looks like this:

FileReader file_read=new FileReader(datei); // Creation of Object from class FileReader
char in [ ] = new char [50]; // Creation of an Array to read chars
int size = file_read.read(in); // save number of chars in a primitive data type
System.out.println(“size: “+size); // Output: 47
for (char c : in)
System.out.print(c); // char-wise output of the values read from file
file_read.close(); // close  FileWriters

In contradiction to here the use of BufferedReader:

BufferedReader file_read =new BufferedReader (new FileReader(datei)); // Creation of Object of BufferedReader with wrapping of class FileReader
String  output;
while ((output = file_read.readLine()) != null) // read from file
System.out.println(output); // line-wise (not char-wise as FileReader) read of file
file_read.close(); // close BufferedReader

Tagged with:
Dec 22

Class BufferedWriter wraps FileWriter

Instead of declaration of class FileWriter

FileWriter write_file = new FileWriter(file);

BufferedWriter will be defined with

Writer write_file = new BufferedWriter(new FileWriter(file));

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs