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
d
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
No comments:
Post a Comment