Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Raymond Osier
Raymond Osier
16,581 Points

How to read and sort a text file with JavaScript

I am working on a project and I am a little stuck on the best way to solve this problem.

I need to build an application that will import into memory a text file with tabbed delimiters of collected data then I need to search threw the file for specific data once that data is found I need to place it into Either ( array, object, or database) a container that i can easily call for a calculation functions. than display the results to the screen and offer option to export to excel format. I originally planned to do this in c# witch is my first language but i am running into trouble sorting threw the data in c# as well , I want to see witch language will be better suited for this task.

1 Answer

From what you've wrote, it sounds like you need AJAX and JASON. Are you pulling data from another source?

Raymond Osier
Raymond Osier
16,581 Points

no i am only pulling data from a local text file ( note this app will be bundled for desktop delpoyment ) . I am currently using the File API here is the start of my code.

if (window.File && window.FileReader && window.FileList && window.Blob){
    console.log('API IS A Go !!');
} else { 
   alert('API Is a NO GO!!');
}
//standard way to select a file in JavaScript
var files = evt.target.files;
function OpenFile(evt){
     // file list object
    // files is a  Fileset of file objects. list some properties
    var output =[];
    for (var i =0, f; f= files[i]; i++){
        output.push('<li><strong>', escape(f.name),'</strong> (' f.type || 'n/a'.')-',f.size,'bytes, last modified: ', f.lastModifiedDate ?f.lastModifiedDate.toLocaleDateString() : 'n/a','</li>');
    }
    document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
}
document.getElementById('files').addEventListener('change' , handleFileSelect,false);



function SortFile(){
    fs.root.getFile(files,{},function(fileEntry)){
                        fileEntry.file(function(file){
        var reader = new FileReader();
        reader.onloadend = function(e){
            var txtArea = document.createElement('textarea');
            txtArea.value = this.result;
            document.body.appendChild(txtArea);
        };
        reader.readAsText(file);
    }, errorHandler);

}, errorHandler);

  }

window.requestFileSystem(window.TEMPORARY,1024*1024,onInitFs,errorHandler);