Tuesday, 12 November 2013

Transposing File by each charcaters

Hi,


I am going to write java program to get transpose of a file

Ex:

let abc.txt  contains

abcd bcd
acd bc
bn


and transpose of file contents becomes

aab
bcn
cd
 b
bc
c
d


here it goes,,


package com.sample;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class TransposeFile {

/**
* @param args
*/
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter bw=null;
try{
br=new BufferedReader(new FileReader("original"));//original file with path
bw=new BufferedWriter(new FileWriter("transpose"));
// transpose file with path if does not exist it will create if valid path
ArrayList< String> lines=new ArrayList<String>();
String line=new String();
while((line=br.readLine())!=null)
{
lines.add(line);
}
int no_of_lines=0;
for(String linu:lines)
{
int j=linu.length();
if(j>no_of_lines)
{
no_of_lines=j;
}
}
int inter=0;
while(inter <no_of_lines)
{
for(int i=0;i<lines.size();i++)
{
if(lines.get(i).length()>inter)
{
bw.write(lines.get(i).charAt(inter));
}
}
bw.newLine();
inter++;
}

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

}
finally
{
try {
br.close();
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}


}

}


Thank you,
Please comment

Tuesday, 5 November 2013

File Extension change

hi,


here i am going to write you how to change file extension of a folder.

Just copy and paste


import java.io.BufferedReader;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;


public class ToChanegExtension
{

public static void main(String[] args)
{
File  path=new File("G:/songs/new");  //just copy paste the path here
File[] listOfFiles = path.listFiles();
try{
for(File file : listOfFiles) {
String name=file.getName();
if(name.matches(".*\\.jpg"))
{
String newName[]=name.split(".jpg");
File newF=new File("G:/songs/new/"+newName[0]+".mp3");

file.renameTo(newF);
}
}
}
catch(Exception e)
{
System.out.println("In catch");
}
}

}