Thursday, 10 October 2013

Write File Using FileOutputStream with new Jdk 1.7 try block feature

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFileExample2 {
public static void main(String[] args) {

File file = new File("d:/java4.txt");
String content = "This is the text content";

//New jdk 1.7 feature
try (FileOutputStream fop = new FileOutputStream(file)) {

// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}

// Get the content in bytes
byte[] contentInBytes = content.getBytes();

fop.write(contentInBytes);
fop.flush();
fop.close();

System.out.println("Done");

} catch (IOException e) {
e.printStackTrace();
}
}
}

Write File Using FileOutputStream

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteFileExample {
public static void main(String[] args) {

FileOutputStream fop = null;
File file;
String content = "This is the text content";

try {

file = new File("d:/java3.txt");
fop = new FileOutputStream(file);

// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}

// Get the content in bytes
byte[] contentInBytes = content.getBytes();

fop.write(contentInBytes);
fop.flush();
fop.close();

System.out.println("Done");

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Write File Using FileWriter

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteFileEx {

public static void main(String args[]) {

String data="THIS IS THE DATA TO WRITE IN THE FILE";

File file = new File("d:/java2.txt");
       FileWriter fr = null;
       try {
           fr = new FileWriter(file);
           fr.write(data);
       } catch (IOException e) {
           e.printStackTrace();
       }finally{
           try {
               fr.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
}

}

Write File Using Files Class

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

//Files class is available from 1.7
public class UsingFilesClass {

public static void main(String args[]) {

String data = "SOME DATA";
try {
           Files.write(Paths.get("d:/java1.txt"), data.getBytes());
           System.out.println("Done");
       } catch (IOException e) {
           e.printStackTrace();
       }
}
}

Write File Using BufferedWriter,FileWriter

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterExample {
public static void main(String[] args) {
try {

String content = "This is the content to write into file";

File file = new File("d:/java.txt");

// if file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();

System.out.println("Done");

} catch (IOException e) {
e.printStackTrace();
}
}
}

How to make a button look like a Link

<html>
<head>
<title>Button Looks Like a Link</title>

<style type="text/css">
button {
background: none;
border: none;
display: inline;
font: inherit;
margin: 0;
padding: 0;
outline: none;
outline-offset: 0;
/* Additional styles to look like a link */
color: #4B0082;
cursor: pointer;
text-decoration: underline;
}
</style>

<script src="jquery.js"></script>
<script>
$(document).ready(function() {
alert("JQuery Loaded");
$("#ButtonId").click(function() {
alert("k fine");
});
$("#ButtonId").mouseup(function() {
$(this).css("color","#4B0082");
});
$("#ButtonId").mousedown(function() {
$(this).css("color","red");

});
});
</script>
</head>

<body>
<button id="ButtonId">Button</button></b>
<a href="#">Link</a>
</body>

</html>