Take the input from a file and write the transpose back into it. For example:
If the file consisted
"abc"
"def"
"xyz"
you need to write a program which writes the transpose i.e.
"adx"
"bey"
"cfz"
into the file.
Solution
If the file consisted
"abc"
"def"
"xyz"
you need to write a program which writes the transpose i.e.
"adx"
"bey"
"cfz"
into the file.
Solution
package com.java;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public static void main(String args[])
throws IOException
{
List<String> list = new ArrayList<String>();
FileReader fr = new FileReader(new File("D:\\ip.txt"));
BufferedReader br = new BufferedReader(fr);
String s = null;
while ((s = br.readLine()) != null)
{
int j = 0;
char[] chararry = s.toCharArray();
while (j < chararry.length)
{
String temps = "";
if (list.size() == 0)
{
temps = "" + chararry[j];
}
else
{
if (j < list.size())
{
temps = list.get(j) + "" + chararry[j];
list.remove(j);
}
else
{
temps = "" + chararry[j];
}
}
list.add(j, temps);
j++;
}
}
FileWriter fw = new FileWriter(new File("D:\\op.txt"));
BufferedWriter bw = new BufferedWriter(fw);
for (String st : list)
{
bw.write(st);
bw.flush();
bw.newLine();
}
bw.close();
fw.close();
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TransposeFile
{public static void main(String args[])
throws IOException
{
List<String> list = new ArrayList<String>();
FileReader fr = new FileReader(new File("D:\\ip.txt"));
BufferedReader br = new BufferedReader(fr);
String s = null;
while ((s = br.readLine()) != null)
{
int j = 0;
char[] chararry = s.toCharArray();
while (j < chararry.length)
{
String temps = "";
if (list.size() == 0)
{
temps = "" + chararry[j];
}
else
{
if (j < list.size())
{
temps = list.get(j) + "" + chararry[j];
list.remove(j);
}
else
{
temps = "" + chararry[j];
}
}
list.add(j, temps);
j++;
}
}
FileWriter fw = new FileWriter(new File("D:\\op.txt"));
BufferedWriter bw = new BufferedWriter(fw);
for (String st : list)
{
bw.write(st);
bw.flush();
bw.newLine();
}
bw.close();
fw.close();
}
}