1

I have a text file that reads

18 8 307 130 3504 12 70 1 10

15 8 350 165 3693 11 70 1 10

18 8 318 150 3436 11 70 1 10

16 8 304 150 3433 12 70 1 10

17 8 302 140 3449 10 70 1 10

where each value is separated by single space. Is there any algorithm by which we can select only specific columns instead of all columns and write them into another file?

For ex. I want to work on 1st,2nd,5th and 6th column so my code should select all attributes in those columns and rewrite them in another file.

1
  • 1
    Please can you show us what code you currently have? Commented Mar 11, 2014 at 6:12

5 Answers 5

1

Algorithm can be like this...

1-First calculate number of rows and columns exists in file by any method.
int col,row;

2-Create a two dimensional matrix 
int matrix[row][col]
and copy all elements from file to matrix.

3-Now you have the matrix you can access any column of your choice and can perform any operation in this.

This can be more refined but idea should like this.

Sign up to request clarification or add additional context in comments.

Comments

1

Answer:

static final String s = "18 8 307 130 3504 12 70 1 10 \n" +
                       "15 8 350 165 3693 11 70 1 10 \n" +
                       "18 8 318 150 3436 11 70 1 10 \n" +
                       "16 8 304 150 3433 12 70 1 10 \n" +
                       "17 8 302 140 3449 10 70 1 10";

public static void main(String... str) {
    List<String> lines = Arrays.asList(s.split("\\n"));
    StringBuilder sb = new StringBuilder();
    for (String s : lines) {
        String[] tempStr = s.split("\\s");
        appendRightColumns(sb, tempStr);
    }

    writeToFile(sb);
}

private static void writeToFile(StringBuilder sb) {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter("C:\\your_file.txt"));
        writer.write(sb.toString());
    } catch (IOException e) {
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
        }
    }
}

private static void appendRightColumns(StringBuilder sb, String[] tempStr) {
    sb.append(tempStr[0]);
    sb.append(" ");
    sb.append(tempStr[1]);
    sb.append(" ");
    sb.append(tempStr[4]);
    sb.append(" ");
    sb.append(tempStr[5]);
    sb.append("\n");
}

Comments

1

please try this:

public void read() throws FileNotFoundException, IOException
{
    FileInputStream fstream = new FileInputStream("screendump");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
     while ((strLine = br.readLine()) != null)   {
     processLine(strLine );
    }
}
protected void processLine(String aLine){
//use a second Scanner to parse the content of each line 
Scanner scanner = new Scanner(aLine);

if (scanner.hasNext()){
  //assumes the line has a certain structure
    switch(i)
    {
        case 1:
         //store in string id = scanner.next().trim(); 
         break;

        case 2: 
         //store in string id+ = scanner.next().trim();
         break;

        case 3:
         break;

        case 4:
         break;

        case 5:
         //store in string id+ = scanner.next().trim();
         break;

        case 6: 
         //store in string id+ = scanner.next().trim();
         break;

        default:
         break;

Comments

0

there are a few ways of doing this, but here is a simple example

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

public class ReadColumns {

    public void SortFileColumns(File sourceFile, File destinationFile)
            throws IOException {

    //Create a file reader
    BufferedReader sourceReader = new BufferedReader(new FileReader(
            sourceFile));
    //Create a file writer
    BufferedWriter destinationWriter = new BufferedWriter(new FileWriter(
            destinationFile));

    try {
        String line;
        while ((line = sourceReader.readLine()) != null) {
            // Split the line on the spaces so that you can select the index
            // representing the column as long as it is not a blank line
            if (!line.trim().isEmpty()) {
                String[] splitLine = line.split(" ");

                String destLine = splitLine[0] + " " + splitLine[1] + " "
                        + splitLine[4] + " " + splitLine[5];
                destinationWriter.write(destLine);
                destinationWriter.write(System
                        .getProperty("line.separator"));
            }
        }
    } finally {

        try {
            destinationWriter.close();
        } catch (IOException ex) {
            // Nothing really to do
        }

        try {
            sourceReader.close();
        } catch (IOException ex) {
            // Nothing really to do
        }
    }

}

}

Comments

0

Try This Code :

  FileReader fr = new FileReader(FileName);
  BufferedReader br = new BufferedReader(fr);
  String s,r2="";
  while((s = br.readLine()) != null) {
     String[] m=s.split(" ");
     for(i=0;i<m.length;i++) {
       if(i==0||i==1||i==4||i==5){
           r2=r2+" "+m[i];
       }
     }
     r2=r2+" \n";
  }

 //Your code to Write a String to another File

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.