Best way to build a String out of characters [duplicate]


I have a large txt-file and I read it character by character. Every character will be added to a StringBuilder, so that I have a big String, representing the whole text file.

My question is: Is there a more efficient way to do that? The performance matters because of the size of the file.

EDIT: I don't just want to read the file character by character into a string. I want to compute each character and write something into the string, depending on the read character, so I really need to read character by character and a solution to read the characters all at once doesn't help.

I'm trying to make a sort of a JSON Parser. It reads the JSON File and prints the hierarchy to the console. So the parser reads the file character by character, computes them and makes a large string. I just want to know the fastest way to build a string out of characters for that.

By : Kangalioo


You can do something like

public String readTextFile(File file) {
      StringBuilder sb = new StringBuilder();
      try(BufferedReader br = new BufferedReader(new FileReader(file))){
          String line;
          while ((line = br.readLine()) != null){
      return sb.toString();
By : DrKarl

Here's a pretty easy way to sort the db return into separate arrays and then display them. @caralot stole my thunder so I came up with this alternative.

Using your current model return $this->db->get('prices')->result_array(); and assigning it to $data.

$data = $this->db->functionName();
//You should check $data validity but I'm skipping that
$month1 = [];
$month2 = [];
$month3 = [];
foreach($data as $row)
    if($row['months'] === '1')
        $month1[] = $row;
    elseif($row['months'] === '2')
        $month2[] = $row;
        $month3[] = $row;
echo "Month 1<br>";
foreach($month1 as $month){
     echo "Level ". $month['level'].' is '.$month['amount'].'<br>';
echo "Month 2<br>";
foreach($month2 as $month){
     echo "Level ".$month['level'].' is '.$month['amount'].'<br>';
echo "Month 3<br>";
foreach($month3 as $month){
     echo "Level ".$month['level'].' is '.$month['amount'].'<br>';

If your table was less ordered than what you show it would be necessary to add a $this->db->order_by('level', 'ASC'); call to the query.

By : DFriend

i did not test this so might have made a stupid error - but basically you can foreach through each of your products - make an array - and then use that array in your view.

    // in your model Note I am returning an object not an array 
    // and always check to make sure something got returned 
    if( ! $products = $this->db->get('prices')->result() )
       return false: 
    $prices = array(); 

    foreach($products as $product)
       // append the months number to the word 'months' to make it clear
       $month =  $product->months . 'month' ; 

       // same with level 
       $level =  'level' . $product->level ; 

       // build the array
       $prices[$month][$level] = $product->amount ; 


    return $prices ; 


so then in your controller - make sure something came back from the model, assign it to data, then pass data to your view

   if( ! $data['prices'] = $this->somemodelname->returnPrices() )
       $this->showError() ; 
       $this->load->view('yourviewname', $data); 

and then in your view you could foreach or just echo out each price if it needs to follow some layout.

 echo '1 month level 1 $' . $prices['1month']['level1'] ; 

and remember your best friend when doing arrays is print_r wrapped in pre tags so like

  echo 'start prices <br> <pre>' ;
    print_r($prices) ;
   echo '</pre>' ;

opinions - its fine to build stuff in the controller and the view while you are developing and building out. but get in the habit of refactoring to your models. keep your controllers as clean and thin as possible. if your views need complicated data structures - build them in a model first. that way if something goes wrong - your controller can decide what to do. AND you don't have to check in your view if $prices is set and valid because you have already done it. this minimizes where things can go wrong.

By : cartalot

This video can help you solving your question :)
By: admin