Tuesday 30 August 2016

Virus affected folder having files is not displaying - How to see the files on Windows


In case of virus affected folders are many time doesn't show the content in the folder. If one open the folder properties then number of files are shown but on entering folder , no file is visible.

To resolve it following 2 steps are to be followed : 

1 .  Show hidden Files,Folders and Drives : 

Go to Control Panel  --> Appearance  and Personalization  --> Folder Options -- > Show hidden files and folders -- > Click on radio button [Show hidden files,folders and drives] 

Now all the hidden files , folders and drives would be visible.

In case of  files are still missing in the folder though size of the folder is showing number of files present in the folder --> follow the step2 

2. Displaying the hidden files of the folder due to affect of  Virus : 

a ) Use some antivirus to scan the folder to clean up the virus.
b)  Go to Start --> Run --> Type cmd and press Enter.

Assuming your drive letter as F: where a folder is affected by virus and folder is not displaying any files though they are exist in the folder.

Copy this command :

attrib -h -r -s /s /d f:\*.*

copy the above command --> Right-click in the Command prompt to paste it --> Enter

Now check your drive for the files in the folder -- > It will display entire files in the folder.

Enjoy the virus free access of the files in your drives. Don't forget to protect your system with an antivirus.

Friday 12 August 2016

Generating the Pdf from HTML

Generating PDF from PDF template is a time & memory consuming process. It's sometime not feasible to use it while processing larger data set.

One of the very good features of creating PDF using HTML is appropriate and simple to use and easy to maintain.

There are many libraries are available for reference  PDF library link 

I will be discuss here itextpdf library of creation as it is simple and easy to maintain. For the Maven based project pom entry :

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->    
<dependency>
    &lt;d&gt;<groupid>com.itextpdf</groupid>
    <artifactid>itextpdf</artifactid>
    <version>5.5.9</version>
</dependency>
   
 

Following is the code snippet to use for conversion of html into pdf :
import com.itextpdf.text.Document;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;
@SuppressWarnings("deprecation")
public static void createPDFUsingHtml(String htmlFilePath,String outputPDFFilePath){
try {
Document document = new Document();
OutputStream file = new FileOutputStream(outputPDFFilePath);
PdfWriter writer = PdfWriter.getInstance(document,file );
///if want to keep password encrypted then add password here else passwordless pdf will be created
writer.setEncryption("".getBytes(), "".getBytes(),PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
document.open();
HTMLWorker htmlWorker = new HTMLWorker(document);
String htmlcontent = readFile(htmlFilePath);
htmlWorker.parse(new StringReader(htmlcontent));
document.close();
} catch (Exception e)
{
e.printStackTrace();
}
}

When run a main  program on the html  test.html


   
public static void main(String[] args){
createPDFUsingHtml("/Users/test/test.html", "/Users/test/test.pdf");
}

Output of the created pdf : test.pdf

Your PDF is ready with a very  simple way and doesn't require any extra effort of creation of the pdf template. Instead just make a simple HTML  template which can be created from any online tool .
Just make your html ready and then your PDF will be created immediately.

Enjoy Coding !!!!



Wednesday 10 August 2016

Reading file in Java


Reading file data is something used day to day life of a developer. Java had continuously  worked on the improvisation of file reading and writing as these are heavy process sometime and memory consuming.

Lets take case by case :

1. Reading a file data using FileReader : 

Method to used :

public static String readFile(String filePath) {
  StringBuilder sb = new StringBuilder();
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(filePath));
   String line = br.readLine();
   while (line != null) {
    sb.append(line);
    sb.append("\n");
    line = br.readLine();
   }
  } catch(Exception e){
   e.printStackTrace();
  } finally {
   if(br!=null){
    try {
     br.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  return sb.toString();
 }   


2. Reading a file data using NIO: 

Method to used :

public static String readFileUsingNio(String filePath){
String content = null;
try {
content = new String(Files.readAllBytes(Paths.get(filePath)));
} catch (Exception e) {
e.printStackTrace();
}
return content;


There are other libraries available of apache common etc where utility of reading file is given.

But as an Architect I feel adding Non Blocking I/O  features in Java 7  is being one of the finest revolution in the I/O  operation . It is always recommended to use Java features are they are fully trusted and robust. Using lesser third party libraries is always advisable as lesser dependencies means lesser chances of getting the bug and code maintainability remains cleans.