Articles tagged with: javaabout
Java Codes »
This example shows how to extend the size of an array. Since arrays are static in size, they cannot be extended in the way collection objects can (for example a Vector).Hence we need to create a new array, add the new data and copy data from the first array.In this example we create an array with three names, then we create another array with the length of 5. We add two names to position 3 and 4 (which is really position 4 and 5 since the first element of an …
Java Codes »
This piece of Java code shows how to convert an array to one of the Colletion types, namely a List.There is a class called Arrays (not to be confused with an array) that has a method called asList() which takes an array as argument and converts it to a List object.Generics can be used to specify the List elements type.
/**
*
* @author www.javabout.com
*/
public class Main {
public void arrayToList() {
String[] cars = {“Dodge”, “Chevrolet”, “BMW”, “Toyota”};
…
Java Codes »
The Arrays class can be used to sort an array of either reference types or primitive types, but the Arrays class does have an overloaded sort method that can sort an array in any way we choose. That overloaded variant of the sort method can only take reference types, and it has an additional parameter which is of type Comparator. This is the parameter that tells how we want the array sorted. In the example below we call the static method reverseOrder on the Collections class. It returns a Comparator …
Java Codes »
Loading parameters into a program is easily handled in Java through the Properties class.
The only thing that has to be done is to create an instance of the Properties class and one instance of
FileInputStream class that points to the configuration file.
The actual loading of the parameters are done through the method load() on the Properties object.
In the example below parameters are read from the configuration file and then printed out.
This is what the sample configuration file looks like:
# System configuration
# Comments will automatically be excluded by the program.parameter1=value1
parameter2=value2
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import …
Java Codes »
This example shows how to delete a line from a file.
The method removeLineFromFile takes two parameters, the first parameter is the file to remove from and the second parameter is the content of the line to remove.
A tempfile is created and written to, except for the content that matches the second parameter.
This way very large files can be handled without demanding so much internal memory.
The original file is then deleted and the tempfile is renamed to the original filename.
For example, the file test.txt is located in the current directory and …
Java Codes »
This code example shows how to change the name of a file.
import java.io.File;
/**
*
* @author www.javabout.com
*/
public class Main {
public void renameFile(String file, String toFile) {
File toBeRenamed = new File(file);
if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
Java Codes »
This code example takes input from user and write the contents of the input to a file for every time the user hits the enter key.
This is continued until the user types the word ‘finished’, then the program exists. A StringBuilder is used to check whether the word ‘finished’ has been typed in.
package com.javadb.examples;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
Java Codes »
This example shows how to read input from console using the BufferedReader class. The BufferedReader has a neat method called readLine() that reads all the input characters into a string when the user hits the Enter button instead of reading byte by byte.
This example takes the input string and parses it into a variable of type Double and then prints out the square root of that number.
This is the code:
package com.javadb.examples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author www.javabout.com
*/
public class Main {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input …
