Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7660923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:31:50+00:00 2026-05-31T13:31:50+00:00

i have a text file that holds a 2-dimensional matrix. it looks like the

  • 0

i have a text file that holds a 2-dimensional matrix. it looks like the following.

01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 19 20

as you can see, each row is delimited by a new line and each column is delimited by a space. i need to transpose this matrix in an efficient way.

01 06 11 16
02 07 12 17
03 08 04 05
04 09 14 19
05 10 15 20

in reality, the matrix is 10,000 by 14,000. the individual elements are double/float. it would be costly, if not impossible, to attempt to transpose this file/matrix all in memory.

does anyone know of a util API to do something like this or an efficient approach?

what i have tried: my naive approach has been to create a temporary file for each column (of the transposed matrix). so, with 10,000 rows, i will have 10,000 temporary files. when i read each line, i tokenize each value, and append the value to the corresponding file. so with the example above, i will have something like the following.

file-0: 01 06 11 16
file-1: 02 07 12 17
file-3: 03 08 13 18
file-4: 04 09 14 19
file-5: 05 10 15 20

i then read each file back in and append them into one file. i wonder if there’s a smarter way because i know the file i/o operations will be a pain point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:31:50+00:00Added an answer on May 31, 2026 at 1:31 pm

    Solution with minimal memory consumption and extremely low performance:

    import org.apache.commons.io.FileUtils;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class MatrixTransposer {
    
      private static final String TMP_DIR = System.getProperty("java.io.tmpdir") + "/";
      private static final String EXTENSION = ".matrix.tmp.result";
      private final String original;
      private final String dst;
    
      public MatrixTransposer(String original, String dst) {
        this.original = original;
        this.dst = dst;
      }
    
      public void transpose() throws IOException {
    
        deleteTempFiles();
    
        int max = 0;
    
        FileReader fileReader = null;
        BufferedReader reader = null;
        try {
          fileReader = new FileReader(original);
          reader = new BufferedReader(fileReader);
          String row;
          while((row = reader.readLine()) != null) {
    
            max = appendRow(max, row, 0);
          }
        } finally {
          if (null != reader) reader.close();
          if (null != fileReader) fileReader.close();
        }
    
    
        mergeResultingRows(max);
      }
    
      private void deleteTempFiles() {
        for (String tmp : new File(TMP_DIR).list()) {
          if (tmp.endsWith(EXTENSION)) {
            FileUtils.deleteQuietly(new File(TMP_DIR + "/" + tmp));
          }
        }
      }
    
      private void mergeResultingRows(int max) throws IOException {
    
        FileUtils.deleteQuietly(new File(dst));
    
        FileWriter writer = null;
        BufferedWriter out = null;
    
        try {
          writer = new FileWriter(new File(dst), true);
          out = new BufferedWriter(writer);
          for (int i = 0; i <= max; i++) {
            out.write(FileUtils.readFileToString(new File(TMP_DIR + i + EXTENSION)) + "\r\n");
          }
        } finally {
          if (null != out) out.close();
          if (null != writer) writer.close();
        }
      }
    
      private int appendRow(int max, String row, int i) throws IOException {
    
        for (String element : row.split(" ")) {
    
          FileWriter writer = null;
          BufferedWriter out = null;
          try {
            writer = new FileWriter(TMP_DIR + i + EXTENSION, true);
            out = new BufferedWriter(writer);
            out.write(columnPrefix(i) + element);
          } finally {
            if (null != out) out.close();
            if (null != writer) writer.close();
          }
          max = Math.max(i++, max);
        }
        return max;
      }
    
      private String columnPrefix(int i) {
    
        return (0 == i ? "" : " ");
      }
    
      public static void main(String[] args) throws IOException {
    
        new MatrixTransposer("c:/temp/mt/original.txt", "c:/temp/mt/transposed.txt").transpose();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file that looks a bit like: random text random text,
I have a text file that looks like this: value1 value2 value3 There are
In my aspx file, I have some code that looks like this: <script type=text/javascript>
I have a text file that stores combinations of four numbers in the following
I have a text file with x amount of lines. each line holds a
I have a file txt file that holds 3 values each seperated by a
I have a class that parses in data from a comma delimited text file.
I have a text file where each odd line holds an integer number (String
I have Text file that contains data separated with a comma , . How
I have a text file that I want to edit using Java. It has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.