Powered by Blogger.
Showing posts with label java program. Show all posts
Showing posts with label java program. Show all posts

About Java | Write String to File using JavaScript Program

Write String to a File:

I am a beginner Java programmer attempting to make a simple text editor. I have the text from the text field in a String variable called "text".

How can I save the contents of the "text" variable to a text file?

String File Example 1:

import java.i o.*;

 public class WriteFile{
   public static void main(String[]args){
      File file = new File (”/home/Parvez/MyJavaBook” , ”MyText.txt”);
      try{
         InputStreamReader isr = new InputStreamReader(System.in);
         BufferedReader in = new Buf feredReader(isr);
         PrintWriter out = new PrintWriter(new FileWriter(file));
         System.out.println (”Write String: ”);
         String str = in.readLine();
         out.println(str);
         in.close();
         out.close();
      }catch(IOException e){
         e.printStackTrac e ();
      }
   }
 }

String File Example 2:

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class WriteToFileExample
{
  public static void main(String[] args)
  {
    try
    {
      // Here we'll write our data into a file called
      // sample.txt, this is the output.
      File file = new File("sample.txt");
      // We'll write the string below into the file
      String data = "Learning Java Programming";

      // To write a file called the writeStringToFile
      // method which require you to pass the file and
      // the data to be written.
      FileUtils.writeStringToFile(file, data);
    } catch (IOException e)
    {
      e.printStackTrace();
    }
  }
}

String File Example 3:

It's a simple example to write String to text file using Java code. Please note that you need the right to write in the target file.

package javafile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class WriteStringToFile {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileWriter fileWriter = null;
        try {
            String content = "Hello! Java-Buddy :)";
            File newTextFile = new File("C:/test/test.txt");
            fileWriter = new FileWriter(newTextFile);
            fileWriter.write(content);
            fileWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(WriteStringToFile.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fileWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(WriteStringToFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

Java Code | Definition of Homogeneous Collection in Java Program

Definition of Homogeneous Collection:-

Homogeneous collection is the collection of objects that have a common class.

 Example of Homogeneous Collection:-

 public class TestHomogeneous{
   public static void main(String[]args){
      TestHomogeneous[] arr = new TestHomogeneous[3];
      arr[0] = new TestHomogeneous();
      arr[1] = new TestHomogeneous();
      arr[2] = new TestHomogeneous();
   }
 }


Homogeneous Collection:-

1. A collection of objects with the same dynamic type. Arrays are the most common homogeneous collection objects. See heterogeneous collection.

2. Browse Related Terms: array, autoboxing, base type, casting, dynamic type, heterogeneous collection, main method, return type.

Homogeneous Collection Programs:-

 class Man{
    public void fly(){
    System.out.print(DIP);
  }
 }
 public class A{
    public static void main(String[]args){
       Man[] arr = new Man[3];
       arr[0] = new Man();
       arr[1] = new Man();
       arr[2] = new Man();
   }
 }

Java Overloading Methods and Program Solution

Definition of Overloading Methods:-

We can declare several methods of same name in a class, which is known as methods overloading. For overloading methods argument lists must be different and return type can be different. In the following code, there are four methods of same name with different argument, which is an example of overloading methods.

Overloading Methods Program:-

 public class ABC{
    int a, b, c;

    public void setValue(){
       a=2;
       b=4;
       c=6;
    }

    public void setValue(int a){
       this.a=a;
       b=4;
       c=6;
    }

    public void setValue(int a, int b){
       this.a=a;
       this.b=b;
       c=6;
    }

    public int setValue(int a, int b, int c){
       this.a=a;
       this.b=b;
       this.c=c;
       int z = a+b+c;
       return z;
    }
  }

Java Solution | Write a Java Program Using String Concatenation with +

String Concatenation with +: 

The + operator performs a concatenation of String objects, producing a new String.

 public class StringConTest{
 public static void main(String[]args){
 String salutation=”Mr.”;
 String name=”Dewan”+”Md.”+”Saju”;
 String title=salutation+name;
 System.out.println(title);
  }
 }

Write a Java Program using This Reference - About Java

Java Program using This Reference:

In Java technology, this keyword is used to resolved ambiguity between instance variables and parameters. It is also used to pass the current object as a parameter to another method.

This Reference Program:

 public class AddNumbers{
 private int num1,num2,result;

 public AddNumbers(int num1,int num2){
 this.num1 = num1;
 this.num2 = num2;
 }

 public void add(){
 result=num1+num2;
 System.out.println(”Result is:”+result);
 }

 public static void main(String[]args){
 AddNumbers addnum = new AddNumbers(10,20);
 addnum.add();
  }
 }