Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java character encoding

  1. Sep 7, 2010 #1
    Ok, so I have been asking a lot of java related questions this past week or so. Now, I have yet another one.

    My program takes a bunch of input in the form of arrays of doubles, and prints it out to a comma seperated csv file for use in a spreadsheet. However, when I try to open the file, I get "gedit has not been able to detect the character encoding.
    Please check that you are not trying to open a binary file.
    Select a character encoding from the menu and try again."

    I want (I think) to use the regular western character set. How do I make java do this?

    here is my program output using println() instead of write(). this is how I know the data is correct.

    Code (Text):
    Please type the full path for the .nmea format fileyou wish to evaluate.
    please not that on Windows this will start with a C:\ given C is the drive letter.
    On Linux it will start with /
    Input file: [as of now this is chosen for you for testing purposes]
    /home/nathan/Desktop/testForPrograms/testNMEA.nmeais your default testing input file
    What would you like to name the output file?
    Pleas note that you NEED to type the full pathname for the file, starting
    with C:\ on Windows or / on Linux.  Do not include the filetype (ex. type
    /examplefile instead of /examplefile.txt)
    Also note that you must have permissions to write to the chosen directory.
    Filename:
    /home/nathan/Desktop/test
    The file /home/nathan/Desktop/test.csv was succesfully created
    Longitude,Latitude,speed
    1346.0447,5408.0683,1.437618599869108
    1346.0042,5408.0548,2.752922888732776
    1345.9666,5408.0379,3.519666321403696
    1345.9294,5408.0205,3.805058492268248
    1345.8873,5408.0056,3.891734664415588
    1345.8398,5407.998,3.986755639889052
    1345.7906,5407.9976,4.06688139093094
    1345.7427,5408.0038,4.266376772980812
    1345.6965,5408.0079,4.174838071948784
    1345.6488,5408.0129,4.189336659714036
    1345.6036,5408.0191,4.207461566365044
    1345.5565,5408.026,4.275158854084337
    1345.5117,5408.0347,4.26628262964756
    1345.4693,5408.0484,4.288425347406208
    1345.4316,5408.0674,4.31528089071634
    1345.3991,5408.0873,4.321029292933596
    1345.3688,5408.1097,4.342781032914805
    1345.3397,5408.134,4.327522610705764
    1345.3172,5408.1585,4.407474993970024
    1345.2966,5408.1846,4.362956000675152
    1345.2779,5408.2098,4.35878745734542
    1345.2608,5408.2359,4.403537436195648
    1345.2446,5408.2619,4.359241711789472
    1345.228,5408.2883,4.378510228439493
    1345.2123,5408.3144,4.382036745103112
    1345.196,5408.3414,4.397782860645064
    1345.1807,5408.3686,4.41382272396454
    1345.1677,5408.3952,4.34225064069304
    1345.1548,5408.4212,4.3450615651350555
    1345.1416,5408.448,4.148299426416156
    1345.1264,5408.4748,4.362366447342327
    1345.1116,5408.5009,4.3427830906925795
    1345.0976,5408.5272,4.290463061848892
    1345.0832,5408.5533,4.323299536264968
    1345.0687,5408.5801,4.338051230696668
    1345.0569,5408.6076,4.325296609596577
    1345.0447,5408.6341,4.321584378488672
    1345.0334,5408.6611,4.323768195153452
    1345.0231,5408.6877,4.2998295518408
    1345.0124,5408.7149,4.296793300732312
    1345.0025,5408.7419,4.293151034068791
    1344.9922,5408.7691,4.296049928510732
    1344.9831,5408.7964,4.273802264085508
    1344.9739,5408.8232,4.299298130730148
    1344.964,5408.8499,4.278798548525636
    1344.9542,5408.8772,4.29753770184278
    1344.9431,5408.9046,4.2933259451797525
    1344.9289,5408.931,4.261407754096216
    1344.9131,5408.9587,4.283286561855093
    1344.8966,5408.9845,4.2879839540732565
    1344.8793,5409.0099,4.26938987408932
    1344.8594,5409.0349,4.270751094088144
    1344.8381,5409.0596,4.273007447419529
    1344.8255,5409.0738,4.288474734072833
    1344.8163,5409.0839,4.28215066852274
    1344.7947,5409.1086,4.248718467440512
    1344.7695,5409.1335,4.238358070782796
    1344.7461,5409.1576,4.28284259629992
    1344.7233,5409.1814,4.2706348296438
    1344.6998,5409.2054,4.265948240758959
    1344.6762,5409.2297,4.234712717452612
    1344.6566,5409.2548,4.297930222953552
    1344.6502,5409.2837,4.237754627449984
    1344.6453,5409.3111,4.203012136368888
    1344.6383,5409.3386,4.141988736421608
    1344.6308,5409.3672,4.228934991902048
    1344.6294,5409.3945,4.234265665230776
    1344.6304,5409.4217,4.23258909078778
    1344.6308,5409.4487,4.225343655238484
    1344.6307,5409.4757,4.222289398574456
    1344.6305,5409.5029,4.227726047458648
    1344.6332,5409.5304,4.275153195195452
    1344.6391,5409.5581,4.290707937404236
    1344.6537,5409.586,4.324509509597256
    1344.6727,5409.613,4.346266394022904
    1344.6944,5409.6373,4.339982969583888
    1344.7169,5409.6634,4.344221477358004
    1344.7381,5409.6886,4.331049641813828
    1344.7626,5409.7135,4.353376016238984
    1344.7837,5409.7398,4.4008844461979395
    1344.8034,5409.7652,4.46275667947782
    1344.8239,5409.7906,4.440420530608228
    1344.8453,5409.8165,4.49564511278274
    1344.8687,5409.8424,4.4735121694685285
    1344.8909,5409.8671,4.4611464683681
    1344.9141,5409.8911,4.422162382846224
    1344.9398,5409.9156,4.463964595032333
    1344.9618,5409.9406,4.449512821711484
    1344.9842,5409.9664,4.454630000595952
    1345.0012,5409.9921,4.487336320567696
    1345.0206,5410.0172,4.47729230724304
    1345.0401,5410.0431,4.476011855021924
    1345.0592,5410.0694,4.523439002758728
    1345.0837,5410.0948,4.491798611674952
    1345.1037,5410.1202,4.560294317171332
    1345.1249,5410.1452,4.428018303952276
    1345.147,5410.1711,4.491721445008352
    1345.1683,5410.196,4.445218239492972
    1345.1839,5410.223,4.448603283934491
    1345.2012,5410.2491,4.447875345046231
    1345.2224,5410.2759,4.41679878507308
    1345.2458,5410.3006,4.450636882821624
    1345.2678,5410.3265,4.367598861782253
    1345.287,5410.3516,4.368142115115116
    1345.3104,5410.3757,4.381897330658789
    1345.3336,5410.4006,4.3151090662720435
    1345.3569,5410.4256,4.283987749632264
    1345.3781,5410.4501,4.3368911584754475
    1345.3998,5410.4748,4.21146857413936
    1345.4245,5410.4995,4.254235884102411
    1345.4475,5410.5244,4.270283978532992
    1345.4669,5410.5505,4.28833943518406
    1345.4877,5410.5762,4.188902983047744
    1345.5099,5410.6009,4.248708692996076
    1345.5311,5410.6268,4.263222714094648
    1345.5509,5410.6515,4.263861654094097
    1345.5719,5410.6769,4.236983475228428
    1345.5929,5410.7014,4.214754330803188
    1345.613,5410.7265,4.201937461925373
    1345.635,5410.7522,4.281650114078729
    1345.6564,5410.7762,4.268137716312624
    1345.6801,5410.8018,4.222870720796176
    1345.7001,5410.8261,4.236422730784468
    1345.7236,5410.852,4.260959672985492
    1345.7463,5410.8781,4.356450336236327
    1345.7678,5410.9026,4.247918506330092
    1345.7907,5410.9264,4.208419976364216
    1345.8121,5410.9513,4.1760609063921725
    1345.8226,5410.9634,4.224680536350168
    1345.8341,5410.9751,4.182718331941976
    1345.8601,5410.9975,4.229958221901164
    1345.8881,5411.0213,4.207279967476312
    1345.9123,5411.0445,4.23140586856658
    1345.9412,5411.0676,4.19774576859566
    1345.9758,5411.0863,4.176858809724815
    1346.0139,5411.1019,4.120780249773264
    1346.0514,5411.12,4.088156755357004
    1346.081,5411.1408,4.116722826443436
    1346.1093,5411.1626,4.077248475366428
    1346.1432,5411.1809,4.0722439598151965
    1346.1802,5411.198,4.0594656742706805
    1346.2153,5411.2166,4.038353388733364
    1346.2476,5411.2379,4.046639030948429
    1346.2815,5411.2589,4.0982702186816
    1346.312,5411.2818,4.029036799852524
    1346.3404,5411.3052,3.991805426551356
    1346.3688,5411.3287,3.8602696988872163
    1346.395,5411.3525,3.8002016222724437
    1346.4175,5411.3775,3.80361701893616
    1346.4307,5411.405,3.805692287823256
    1346.4366,5411.4326,3.837385666684764
    1346.4396,5411.4615,3.9077585799573
    1346.4411,5411.489,3.713724540124932
    1346.4448,5411.516,3.939924733262844
    1346.4489,5411.5432,3.9523537110298843
    1346.4562,5411.5702,3.98570720211218
    1346.4594,5411.5986,3.919736389946952
    1346.46,5411.6267,3.97031759656992
    1346.4573,5411.6553,3.971565638791064
    1346.4535,5411.6826,3.964763654352496
    1346.4568,5411.7105,3.8779295477608478
    1346.4541,5411.7375,4.005166577650924
    1346.4543,5411.7661,4.001259886543188
    1346.4543,5411.7941,4.146809595306332
    1346.4509,5411.8213,4.005494278761752
    1346.4477,5411.8492,3.890025165528176
    1346.4459,5411.8771,3.8980942266323164
    1346.4459,5411.9048,3.8778981666497643
    1346.4422,5411.9318,3.912466261064344
    1346.4392,5411.9607,3.8966985388557442
    1346.4343,5411.9877,3.9417283754835077
    1346.4283,5412.0147,3.926976166607364
    1346.4219,5412.0417,3.893469371080756
    1346.4151,5412.0694,3.9409531077063997
    1346.4084,5412.0969,3.677869820155908
    1346.3951,5412.123,3.5134960747423603
    1346.382,5412.15,3.3116815771389363
    1346.3752,5412.1774,3.151773723943752
    1346.3735,5412.2049,2.587214159987048
    1346.3763,5412.2324,2.393039676821468
    1346.3855,5412.2594,2.1512240370303797
    1346.3564,5412.2399,0.17213619762906401
    1346.3542,5412.2397,0.44897624405656
    1346.3213,5412.2587,0.567389008398704
    1346.2847,5412.242,0.932679031416452
    1346.244,5412.2289,0.85912479370222
    1346.2314,5412.2321,0.099052676581092
    1346.2004,5412.2381,0.064433137722112
    1346.2061,5412.2106,1.325393058854952
    1346.2001,5412.1838,1.221707295611196
    1346.1948,5412.1568,1.28985626555232
    1346.1889,5412.1294,1.1953101223006681
    1346.187,5412.125,1.467890568731844
    1346.1945,5412.1568,1.6402958208051202
    1346.2004,5412.1836,1.344092085505464
    1346.2092,5412.2104,1.295055241103384
    1346.2388,5412.2314,1.2785086500065679
    1346.285,5412.2335,1.3231058388569281
    1346.3018,5412.2589,0.9571552691730839
    1346.324,5412.2825,1.152762994559648
    1346.3267,5412.3097,0.727267023816136
    1346.2855,5412.3235,1.288077316664968
    1346.2405,5412.3301,1.280619930004744
    1346.2008,5412.3443,0.773008851554396
    1346.1539,5412.3447,1.006298603575072
    1346.2005,5412.3415,0.859486448146352
    1346.2401,5412.3273,0.891024464785772
    1346.2867,5412.3241,1.257226597802732
    1346.3287,5412.3127,0.9268164225326281
    1346.3242,5412.2857,1.157671308999852
    1346.3166,5412.2699,0.032890491082696004
    1346.3102,5412.2598,0.121117198784252
    1346.3463,5412.2422,1.3359540888458281
    1346.3775,5412.2214,2.191519955884456
    1346.3696,5412.1941,3.0078506885125362
    1346.3705,5412.166,3.3361768493399957
    1346.3842,5412.1397,3.5976864513362923
    1346.3929,5412.1117,3.657770475728828
    1346.3999,5412.0845,3.7243498756713076
    1346.4071,5412.0561,3.773224670073528
    1346.4176,5412.0277,3.7815679300663203
    1346.4278,5412.0003,3.8184448511455718
    1346.4327,5411.9728,3.8098670044863163
    1346.4365,5411.9453,3.676738556823552
    1346.4374,5411.9165,3.813609073371972
    1346.4425,5411.8877,3.810284733374844
    1346.4462,5411.86,3.7791603300684
    1346.4476,5411.8483,3.78123868562216
    1346.4503,5411.8314,3.778247705624744
    1346.4516,5411.8039,3.92396100772108
    1346.4546,5411.7757,3.79483545227708
    1346.46,5411.7485,3.815213625592808
    1346.4647,5411.7199,3.8596966077766
    1346.4683,5411.6929,3.8515750733391716
    1346.4719,5411.6654,3.871727919988428
    1346.4754,5411.6382,3.810506973374652
    1346.4785,5411.6103,3.792662953390068
    1346.4812,5411.5816,3.839871976682616
    1346.4835,5411.5535,3.816054742258748
    1346.4857,5411.5249,3.833229470021688
    1346.4889,5411.4977,3.857505588889604
    1346.4882,5411.4695,3.90893922995628
    1346.4842,5411.442,3.887238419975028
    1346.484,5411.4137,3.8676452888808437
    1346.4723,5411.3864,3.783271255620404
    1346.4445,5411.364,3.691897176810456
    1346.4081,5411.345,3.636679282413716
    1346.3739,5411.326,3.641224399076456
    1346.3413,5411.3049,3.865231001105152
    1346.3099,5411.2836,3.73597014677238
    1346.2762,5411.2635,3.8693151755460677
    1346.2399,5411.2447,3.75936450786328
    1346.2045,5411.2244,3.802648320048108
    1346.1699,5411.2035,3.808449195598652
    1346.1364,5411.1827,3.802026356715312
    1346.1011,5411.1626,3.7538049067569723
    1346.0669,5411.1424,3.7468362423185484
    1346.0329,5411.1225,3.7383041812148083
    1346.0,5411.1024,3.727536859001888
    1345.968,5411.0813,3.672591620160468
    1345.9364,5411.06,3.647860732404056
    1345.9061,5411.0383,3.629052643531416
    1345.8768,5411.0172,3.604345934663872
    1345.8478,5410.9953,3.644294603518248
    1345.8199,5410.9726,3.612828609100988
    1345.795,5410.9486,3.613220101322872
    1345.7691,5410.9241,3.6696294490519157
    1345.7459,5410.9,3.628073655754484
    1345.7222,5410.876,3.6488417779587636
    1345.6999,5410.8513,3.6489873657364162
    1345.6771,5410.8275,3.634356051304612
    1345.653,5410.8037,3.66965054127412
    1345.6291,5410.779,3.666027823499472
    1345.6053,5410.7536,3.670935109050788
    1345.5818,5410.7286,3.676815723490152
    1345.5592,5410.7048,3.650007509068868
    1345.5374,5410.6799,3.647666786848668
    1345.5161,5410.6549,3.650458676846256
    1345.4944,5410.6304,3.640770659076848
    1345.4717,5410.6068,3.642298044631084
    1345.4484,5410.583,3.580791581350888
    1345.4248,5410.5595,3.589864838009716
    1345.4025,5410.5354,3.572782195802252
    1345.3821,5410.5107,3.5790018291302124
    1345.361,5410.4865,3.56520031358658
    1345.3393,5410.4608,3.549344106933612
    1345.3182,5410.4352,3.5578591913707
    1345.2964,5410.4095,3.542531833606164
    1345.2748,5410.3837,3.563357059143728
    1345.2521,5410.3587,3.4844531136563397
    1345.2298,5410.3338,3.4963846236460316
    1345.2083,5410.3088,3.474299523665112
    1345.1917,5410.2912,3.453222220349988
    1345.1857,5410.2848,3.4801250925489677
    1345.162,5410.2601,3.4495470292420523
    1345.1394,5410.2351,3.475994618108092
    1345.1186,5410.21,3.43748690814136
    1345.0966,5410.1848,3.455159618126092
    1345.0745,5410.1603,3.433980454811056
    1345.0524,5410.1349,3.470747284779292
    1345.0316,5410.1095,3.457024993680036
    1345.01,5410.0836,3.5086525803021
    1344.9907,5410.0589,3.52087320806932
    1344.9684,5410.034,3.535364593612356
    1344.9462,5410.009,3.499789731420868
    1344.924,5409.9834,3.506770742525948
    1344.9035,5409.9575,3.473039649221756
    1344.8833,5409.9316,3.479256710327496
    1344.8639,5409.9053,3.436621098142108
    1344.8445,5409.8804,3.43504586925458
    1344.8259,5409.8552,3.37819461374814
    1344.8066,5409.8302,3.38467404152032
    1344.7866,5409.8051,3.3740523070850523
    1344.7659,5409.7803,3.411125745941912
    1344.7455,5409.7552,3.379652034857992
    1344.7247,5409.7293,3.406263731501668
    1344.7053,5409.7048,3.413451549273236
    1344.6833,5409.681,3.455980671458716
    1344.6612,5409.6558,3.4580790903457923
    1344.638,5409.6304,3.4800633592156878
    1344.6156,5409.6053,3.52595849139826
    1344.596,5409.5805,3.5106172436337357
    1344.585,5409.5526,3.50463219697224
    1344.5823,5409.5254,3.5339009991691763
    1344.584,5409.4979,3.560987528034664
    1344.5871,5409.4707,3.5536551513743317
    1344.5912,5409.4425,3.572498222469164
    1344.5948,5409.4137,3.587757673567092
    1344.5978,5409.3855,3.5983053280024238
    1344.6025,5409.3585,3.615115314654568
    1344.6099,5409.3311,3.6081903779938838
    1344.6189,5409.3036,3.602094725776928
    1344.6312,5409.2759,3.6232558835364244
    1344.649,5409.2507,3.6649762990559362
    1344.6702,5409.2251,3.688682927924344
    1344.6923,5409.2001,3.715803410123136
    1344.7134,5409.175,3.69277481903192
    1344.7353,5409.1498,3.742669756766592
    1344.7566,5409.1239,3.729692895666692
    1344.7773,5409.099,3.6928771934762756
    1344.7982,5409.0733,3.686388505704104
    1344.8182,5409.0482,3.66868081349718
    1344.8368,5409.0223,3.6677388657202163
    1344.8549,5408.9962,3.661070122392644
    1344.872,5408.9708,3.6770240734899717
    1344.889,5408.9447,3.6991981723597043
    1344.9034,5408.9177,3.441763999248776
    1344.9136,5408.8909,3.67505735238056
    1344.9262,5408.8648,3.63899736907838
    1344.9367,5408.8374,3.6373058757465078
    1344.9484,5408.8103,3.709597666795164
    1344.958,5408.7824,3.7243663378935157
    1344.9701,5408.7556,3.709436645684192
    1344.985,5408.7286,3.6496293924025283
    1345.0017,5408.7035,3.68629539125974
    1345.0127,5408.6824,3.681286245708512
    1345.0159,5408.6767,3.74130493565666
    1345.0306,5408.6497,3.669178795718972
    1345.0445,5408.6236,3.66384503572358
    1345.0602,5408.597,3.668238905719784
    1345.0744,5408.5707,3.662850100168884
    1345.0892,5408.5435,3.63275715797266
    1345.1055,5408.5167,3.501991553641188
    1345.1186,5408.4901,3.6097758957702917
    1345.1337,5408.4643,3.639560171300116
    1345.1459,5408.4377,3.595029345783032
    1345.1582,5408.4116,3.580330639129064
    1345.1713,5408.3855,3.5698714691381
    1345.1873,5408.3601,3.605226663552
    1345.206,5408.335,3.653552031288028
    1345.2241,5408.3098,3.554840945817752
    1345.2426,5408.2846,3.4999142269763164
    1345.2606,5408.2587,3.594082253561628
    1345.2783,5408.2329,3.695640274585
    1345.2982,5408.2079,3.814579315593356
    1345.3222,5408.1832,3.865459414438288
    1345.345,5408.1596,3.8553413211136958
    1345.369,5408.1342,3.8912788666382037
    1345.3961,5408.1102,3.861766217774812
    1345.4258,5408.0887,3.841014043348296
    1345.4547,5408.0653,3.807154853377548
    1345.4903,5408.047,3.929345183271984
    1345.5312,5408.0328,3.9095591355112997
    1345.5768,5408.0264,4.011474180978808
    1345.6234,5408.0228,3.838562201128192
    1345.6708,5408.0191,3.704610642355028
    1345.7171,5408.0166,3.644944861295464
    1345.7653,5408.0145,3.625180420201428
    1345.8115,5408.0134,3.635292340192692
    1345.8576,5408.0136,3.637260604635436
    1345.9037,5408.0168,3.5283403691739803
    1345.9502,5408.0241,2.935744611908164
    1345.9963,5408.0301,2.431808210121308
    1346.0261,5408.0511,1.84052017063214
    1346.0351,5408.0777,0.987967919146464
    1346.0445,5408.1042,0.732899676033492
    The analysis is done.
    And here is the method that is doing the file writing. You can see the println statements for my own error checking:

    gpsLat and gpsLond and speed are the arrays that hold the gps and speed data.
    Code (Text):

    BufferedWriter fileStreamOut = new BufferedWriter(new FileWriter(outputFileName));

    String tempString;
           
            fileStreamOut.write("Longitude,Latitude,speed");
           
            System.out.println("Longitude,Latitude,speed");
           
            for(int i = 0; i<gpsLong.length; i++){
                tempString = Double.toString(gpsLong[i]) + "," + Double.toString(gpsLat[i])+ ","+
                        Double.toString(speed[i]);
               
                fileStreamOut.write(tempString);
                fileStreamOut.newLine();
                System.out.println(tempString);
            }
    If you need any more portions of the code, please just ask.

    Thanks for all the help,
    Nkk
     
  2. jcsd
  3. Sep 8, 2010 #2

    Filip Larsen

    User Avatar
    Gold Member

    You should be able to control the encoding by using a OutputStreamWriter-FileOutputStream combo instead of a FileWriter (FileWriter is just a wrapper for for such a combo anyway). The encoding can be provided in the OutputStreamWriter constructor.
     
  4. Sep 8, 2010 #3
    Thank you very much. That is exactly what I was looking for. Is there any way to buffer the output to this writer? I remember reading that as of some past version of java, buffered readers and writers were preferred. Is that not the case here?

    Thanks,
    Nkk
     
  5. Sep 8, 2010 #4

    Filip Larsen

    User Avatar
    Gold Member

    If you just replace the FileWriter you should still have the BufferedWriter around that. That is, you have something like

    Code (Text):

    try {
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFileName), "ISO-8859-1"));
      // use out
      out.close();
    } catch (Exception ex) {
      ex.printStackTrace(System.err);
    }
     
    where I tried to indicate that it probably is a good idea to also consider exception handling and to use close (or flush) when you are done with the output to ensure all characters gets into the file.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook