Read a utf-8 text file

Hi, until now, I used the default encoding for reading and writing text files, but I discovered it causes question marks trying to read or write the non-English characters, so I'm trying to change my code to use:

inputstramreader and OutputStreamWriter inputreader and outputwriter, I have used before.

However, I discovered the onlyworks linereader with InputStreamand not with inputstreamreader.

also bufferedread is not available in api.

It looks something like this:

View linereader takes inputstream and not inputstreamreader.

          fc = (FileConnection) Connector.open(targetFolder + "file.txt", Connector.READ_WRITE);
            InputStream is = fc.openInputStream();
            InputStreamReader reader = new InputStreamReader(is, "UTF-8");
            String line = null;
            LineReader lineReader = new LineReader(is);
            try
            {
                lineReader.readLine();  // skip header line
                line = new String(lineReader.readLine());   // get line from file
            }

so the question is, I have to manually write a function to read a line in the file? or is it an another workaround?

You need to specify the encoding of the byte array and do not rely on the default encoding when you create a string to a byte array.

Use this:

line = new String(lineReader.readLine(), "UTF-8"));

Tags: BlackBerry Developers

Similar Questions

  • OMBPlus - TCL - reading variables out of text file

    Hello

    I want to read a text file variables.

    For example to connect

    OMBCONNECT $login/$pass@$host:1522:$service

    and these 4 variables in a text file.

    I'm a newbie, so this might be a stupid question


    Thank you!

    use

    OMBCC ' / / $i / / $I

    It's the double slash

    Published by: Katia on March 10, 2010 09:17

    Published by: Katia on March 10, 2010 09:21

  • Read/write to DB text file

    Hello

    What is the effective way of reading the text file and write to the DB table.

    Forms Builder (10.2.0.3.0) - version
    DB 10 g 2


    Rgds


    sexy

    Hello

    TEXT_IO is a software package, integrated forms. Webutil has too (but it's CLIENT_TEXT_IO, which can be read from the client computer), in which, plain TEXT_IO reads the file from the server computer.

    As I said before, it depends on the number of records / size of the file. If he records little (say a few hundred), then you can go for TEXT_IO. However, if the size of the file is very high / you have more records (say thousands / millions), then SQL Loader is the best bet.

    If you are worried about the performance (i.e. If you have more records to be inserted), you can create a batch script (which loads the data into tables using SQL Loader) and call the command script using the HOST of shapes command.

    You can see the SQL Loader manual (which I gave the link before), for orders / check the syntax of the file etc and create a command script. Probably, if you're looking for the PL/SQL forum / forum SQL Loader, you will get more ideas and samples.

    HTH.

    Arun-

  • Read data from a text file

    Hi, using Cp 8.01.

    I would like to read the textual data to a text or XML file into a variable in order to display a message (using a form) on my project of Cp can I do this without using Javascript?

    Thank you.

    You need JS.

  • How to read and write a text file to the folder of the user preferences?

    I'm trying to code for the user to record the plug-in, the first time that the user opens Acrobat after you have installed the plug-in. I want to write a file of registration of preference of the user folder access later to check that they are already registered.

    Is there an easy way to do this? I wrote the code to access the preference of the user folder to search for the file, but I have not been able to code for the write file. All I've found is how to write a PDF file.

    Any suggestions?

    Gregory

    ASFileRead/ASFileWrite

  • Sending file to server UTF-8 text

    I am trying to download a UTF-8 text file to a server. The download works fine, but when I check in the server, the file is ASCII.

    This is how I create my file:

    private void writeToFile(String line) throws IOException{
            FileConnection fc = null;
            try {
    
                FileConnection fileConnection = (FileConnection)Connector.open(Constants.FOLDER_FILES+fileName);
                if (!fileConnection.exists()){
                    fileConnection.create();
                }
                fileConnection.close();
    
                fc = (FileConnection)Connector.open(Constants.FOLDER_FILES+fileName, Connector.READ_WRITE);
                long byteOffset = fc.usedSize();
                OutputStream outStream = fc.openOutputStream(byteOffset);
                outStream.write(line.getBytes("UTF-8"));
                outStream.flush();
                outStream.close();
    
            }catch (IOException ioe){
                UtilsFiles utilsFiles = new UtilsFiles();
                utilsFiles.writeInLog("UtilsFiles error: writeToFile ioexception "+ioe.getMessage());
            } finally {
                if (fc!=null) {
                    try {
                        fc.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    

    And in the way which I download it:

    public class HTTPUploader {
    
        private String _connectionURL = null;
        private String fileName;
        public HttpConnection httpConnection = null;
    
        public HTTPUploader(String _fileName) {
            fileName = _fileName;
        }
    
            public void run (){
    
            httpConnection = null;
            _connectionURL = null;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int rc = -1;
            OutputStream os = null;
    
            try {
                httpConnection = (HttpConnection)Connector.open(_connectionURL);            
    
                httpConnection.setRequestMethod("POST");
                httpConnection.setRequestProperty("Connection", "Keep-Alive");
                httpConnection.setRequestProperty("User-Agent", "BlackBerry");
                httpConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
                httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LANGUAGE, "en-US");
                httpConnection.setRequestProperty(HttpProtocolConstants.HEADER_CACHE_CONTROL,"no-cache, no-store, no-transform");           
    
                os = httpConnection.openOutputStream();
                os.write((twoHyphens + boundary + lineEnd).getBytes());
                os.write(("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + fileName +"\"" + lineEnd).getBytes());
                os.write(lineEnd.getBytes());
                byte [] postDataBytes = getData();
                os.write(postDataBytes);
                os.write(lineEnd.getBytes());
                os.write((twoHyphens + boundary + twoHyphens + lineEnd).getBytes());
                os.flush(); 
    
                rc = httpConnection.getResponseCode();
                InputStream in = httpConnection.openInputStream();
                ...
            }
            catch (ClassCastException cce)
            {
                ...
            }
            catch (IOException ioe)
            {
                ...
            }
            finally
            {
                ...
            }
           }
    
           private byte[] getData() throws IOException
           {
                    FileConnection fileForUpload = (FileConnection)     Connector.open(Constants.FOLDER_FILES+this.fileName, Connector.READ);
            InputStream is = fileForUpload.openInputStream();
            return IOUtilities.streamToBytes(is);
           }
    
    ...
    
    }
    

    I get the file correctly, but it is ASCII, and I don't know why.

    Thanks for your help

    Makes you think the ASCII file?

    Remember that MARC and UTF-8 are not actually different assuming that you only ASCII characters in the file.

  • Reading a text file

    I did a code that reads values in the text file. Now I want to produce a chart that can watch the number of items in the files (x axis) and the values in the text file (y-axis).


  • Reading a text file and store it in a table, guard return 0.

    Hello, I have an application where I need to read the speed and rate of a text file. For this application, I use a myRIO NEITHER and I want to save the text file on the drive the myRIO aboard in the tmp folder. I simplified my request to isolate the possible problem and I will attach a sample text file and my VI is used.

    Overview:

    I use a myRIO project because in my real application I myRIO VI of PWM and encoder. I noticed that my problem goes away when just open the main VI and run the application in LabView. My problem then that chairs the VI on a project myRIO folder.

    To address the issue:

    My question is, when running the VI of the hand of a project myRIO folder everything works as it is supposed to, but when you open the text file in my Subvi, it returns 0, as if nothing is in the text file. Now if I open the VI outside myRIO se project file it works just assumed that when X = 1, it opens the text file written data in a table and then close the text file. I can't understand why he reads nothing in the text file executing the main VI of a project of myRIO and when I use run to highlight one can see it opens the text file and then returns nothing. The text "test" file must be placed in the tmp folder on board the myRIO to validate my way.

    Is there a way to fix this?

    I assume because I use a myRIO project I can't access the file text, or because of how LabView?

    Any help is appreciated, thanks in advance!

    You don't have your son wired error upward.  You're files showing error functions?

    What context are you running VI when you are within the project and what context from outside the project?

    My suspicion is that in one case, you run on your PC, the other on the cRIO.  Your file exists only on one device or the other.  And also that when you change your PC for the cRIO, your file path must change.  It's very strange that your watch VI access you the disk R:\.  I bet that the R:\ car n does not exist on your cRIO.

  • Can not read the text file: 7 error file not found

    Hello

    I read a (header.txt) text file that is placed in the same directory as the vi. I use winXP & Labview 8.6. The VI reported an error:

    Error 7 open/create/replace the file in OpenTextFile.vi.

    However, I copied the path of the file at the end of the error message and pasted into the address bar of IE, and I found that IE can open the file.

    Why does this work?

    Thank you

    Ryan

    Never mind. I solved it. I fed the wrong file name in the diagram.

  • Reading data from a text (JS CS3) file tabs-delimited

    Hi - I'm working on a script to read data from a text file and use in a dialog box. But I hit a wall.

    I used a script from a previous post that defines a variable text document when the user script he chooses from a drop-down list.

    var myDialog = app.dialogs.add({name:"Map",canCancel:true});)
    {with (MyDialog)}

    {with (dialogColumns.Add ())}
    {with (borderPanels.Add ())}
    staticTexts.add ({staticLabel: "choose the location :"});})
    {with (dialogColumns.Add ())}

    var file = File("~/Desktop/myPlacesfile.txt");

    leader. Open ("r");
    var str = file.read ();
    leader. Close();
    var myPlaceList = str.split (/ [\r\n] + /);
    var myPlaceMenu = dropdowns.add ({stringList:myPlaceList, selectedIndex:0});})
    }
    }}}
    Ditto var = myDialog.show ();
    if(myResult == true) {}
    If (myPlaceMenu.selectedIndex == 0) {}
    myPlace var = ' - undefined ";
    } else {}
    myPlace var = myPlaceList [myPlaceMenu.selectedIndex];
    Alert (myPlace);
    }

    myDialog.destroy ();
    }

    That's what I do now:

    The text file is in this format:

    Value1 value2 [TAB]

    Value1 value2 [TAB]

    Value1 value2 [TAB]

    I need to have the drop down dialog box show only the value 1, and after that the user selects, the script returns only the value 2. (The alert is just there to test - I'm doing something else with the variable).

    Is there a way to view the first part of a tab-delimited line in the menu drop down and return the second half as a variable?

    Any help would be greatly appreciated.

    Thank you

    One of the possibilities is that it. Create a table to the left of the values of the tab of the dialog box. Then create an object that you use it as a table of correspondence. Roughly as follows:

    same thing as what you have

    leader. Open ("r");
    var str = file.read ();
    leader. Close();
    var array = str.split (/ [\r\n] + /);

    'pairs' are the table of correspondence

    pair of var = {};

    as before, 'myPlaceList' will be used for the menu drop-down
    var myPlaceList = [];

    var v;
    for (var i = 0; i)< array.length;="">
    {
    v = table [i] .split ('\t');
    pairs [v [0]] = v [1];
    myPlaceList.push (v [0]);
    }

    Add the drop-down list as before:

    var myPlaceMenu = dropdowns.add ({stringList:myPlaceList, selectedIndex:0});})

    the table of 'pairs' correspondence is used as follows: pairs ['value1'] returns 'value2 '.

    so in your script that would be:

    myPlace var pairs = [myPlaceList [myPlaceMenu.selectedIndex]];

    Peter

  • Wrong exit from text file

    I´d as read a SD card text file and display the contents.

    The content of the file:

    Archer 1
    Archer 2
    Archer 3
    Archer 4
    

    my code:

            try {
                FileConnection con = (FileConnection) javax.microedition.io.Connector.open("file:///SDCard/test.txt");
                if(con.exists())
                {
                    InputStream input = con.openInputStream();
                    StringBuffer buffer = new StringBuffer();
                    while(input.read() != -1)
                    {
                        buffer.append(input.read());
                    }
                    showMessage("OutPut: "+buffer.toString());
                }
            } catch (IOException e) {
                showMessage("Es ist ein Fehler aufgetreten.");
            }
    

    And the output is:

    11410411449101141041145010114104114511011410411452

    What should be changed?

    The problem is that you read the file in bytes and the StringBuffer does not accept the bytes as something that you can add. Therefore, it is interpretting the byte value read from the InputStream as an integer.

    Here are two quick fixes on top of my head, I have not tested this code. One reads the InputStream using an InputStreamReader that can give you the contents of the file as an array of char that can be added directly to the StringBuffer. The second way still reads the file as bytes but then uses the StringUtilities class to add the array of bytes to the StringBuffer.

    try
    {
        FileConnection con = (FileConnection) javax.microedition.io.Connector.open("file:///SDCard/test.txt");
        if(con.exists())
        {
            InputStream input = con.openInputStream();
            InputStreamReader reader = new InputStreamReader(input);
            StringBuffer buffer = new StringBuffer();
    
            char[] charBuffer = new char[1024 * 8];
            int lengthRead;
            while ((lengthRead = reader.read(charBuffer, 0, charBuffer.length)) > 0)
            {
                buffer.append(charBuffer, 0, lengthRead);
            }
            showMessage("OutPut: "+buffer.toString());
        }
    }
    catch (IOException e)
    {
        showMessage("Es ist ein Fehler aufgetreten.");
    }
    
    try
    {
        FileConnection con = (FileConnection) javax.microedition.io.Connector.open("file:///SDCard/test.txt");
        if(con.exists())
        {
            InputStream input = con.openInputStream();
            StringBuffer buffer = new StringBuffer();
    
            byte[] byteBuffer = new byte[1024 * 8];
            int lengthRead;
            while ((lengthRead = input.read(byteBuffer, 0, byteBuffer.length)) > 0)
            {
                StringUtilities.append(buffer, byteBuffer, 0, lengthRead);
            }
            showMessage("OutPut: "+buffer.toString());
        }
    }
    catch (IOException e)
    {
        showMessage("Es ist ein Fehler aufgetreten.");
    }
    
  • USB for my iPhone, so it can read a text file

    Hi, I would like to be able to connect a standard USB to my iPhone, so it can read a text file from USB, any suggestions on cable/apps? Thank you, Tony

    Hello. In fact, you can use any USB adapter to connect your iPhone to the computer and transfer text files. Apple recommends the adapter like this http://www.apple.com/shop/product/MD820AM/A/lightning-to-micro-usb-adapter

  • Problem reading text file

    CVI programmer for several years, but it has been awhile. Trying to read in a text file that displays fine in say Notepad. The first line of text in the file is:

    All temperatures in ° F.

    If I use the scan function, say:

    public static char cdum [2048];

    ScanFile (DataFile, "%s > %s", cdum); I get these results:

    cdum [0] is - 2

    cdum [1] is 65 (which is the ASCII character 'A')

    The rest of the cdum is all zeros.

    In a hex editor in decimal display grouped by bytes, the line is:

    0 255 254 65 0 108 0 108 0 32 0 116 0 101 0 109 0 112... as the display looks like

    ybA.l.l.. t.e.m.p.e.r.a.t.u.r.e.s.. i.n.. °. F....

    So I get the scan function is to see the start character, then by pulling the 65 and placing in the location cdum [1], then strikes the character 0 and leaves the function of scan as she found an end of line. Trying to read in the entire line and the subsequent data float line, but is not serious, I can understand that our after I get over the initial problem above.

    Can someone help me with this? I have no problem reading in ordinary text files, but this one is as it is grouped in words rather than in bytes. Have you tried a bunch of different Scan function modifiers source and target [xt0, w #, etc.], but nothing seems to work.

    Data file attached.

    Thank you-

    Scott

    Hello

    I downloaded your file, it is opened in Excel because of its extension and the settings on my computer. I then wanted to save it as a text file normal to see in Notepad and read with a simple C program. In doing so, Excel told me: some features in your workbook may be lost if you save it in Unicode text.

    This is consistent with your observation of 16-bit data.

    LabWindows/CVI, unfortunately, does not support the Unicode character set and functions of the library operated at only 8-bit characters.

    So I would say to convert your file to 8-bit characters; You can also support the pursuit of the Unicode support in CVI here

  • read and medium n data from text file

    I acquire [a waveform of reading] oscilloscope digital data that is stored in a column in a text file.

    I need to read selectable amount of data at average/parcel them points.

    So basically I want to have a text file with millions of data points, but I want to draw not to say that all 1000. data point. I tried a lot of things I found here on the forum, but as I'm new to LabView, none of them really worked.

    Does anyone have a suggestion.

    Thank you.


  • Reading text file takes a long time after the first time

    Dear experts LabVIEW,

    I'm having a problem with playback of text file. I'm reading only each Nth line from a file to preview with this sub vi:

    I seems to work the first time I do it well. The loop takes almost no time to perform an iteration.

    Then when I load the same file with exactly the same parameters an iteration takes about 50ms.

    Subsequent attempts always seem to take longer execution time.

    Only when I restart the calling vi fast for a single file.

    When executing the sub vi alone it is still fast, but I don't see how the main vi (too complex to post here) could hinder the execution of the Sub vi.

    I have no one else to open the file in the main vi, I do not use too much memory...

    Just now I have not now where to look. Does anyone have an idea?

    Concerning

    Florian


Maybe you are looking for