Powered by Blogger.
Showing posts with label about java. Show all posts
Showing posts with label about java. 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 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;
    }
  }

OOPs in Java - Definition of Encapsulation Example with Program

Definition of Encapsulation:-

Encapsulation is the technique of hiding some members of a class from other classes but provides a public interface to access that members.

General Definition:-

In general, encapsulation is one of the 4 fundamentals of OOP (object-oriented programming). Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing.

This mechanism is not unique to object-oriented programming. Implementations of abstract data types, e.g. modules, offer a similar form of encapsulation. This similarity stems from the fact that both notions rely on the same mathematical fundament of an existential.

Example of Encapsulation Program:

 class MyNumber{
   private int number;

   public void setNumber(int number){
     this.number=number;
   }

   public int getNumber(){
     return number;
   }
   }

   public class EncapTest{
     public static void main(String[]args){
       MyNumber my = new MyNumber();
       my.setNumber(45);
       System.out.println(my.getNumber());
   }
  }

At line 2, attribute number is a private member of MyNumber class, so this attribute will not be accessible form other classes. But from the EncapTest class we are accessing the number variable of MyNumber class using set and get methods of MyNumber class.

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);
  }
 }