Soru & Cevap

karakter kodlama dönüştürme sorunu ...

21.10.2015 - 10:18

windows1252 ile kodlanmış dosyayı utf8 veya tüm dillerde gösterebilecek şekilde dönüştürmek istiyorum ama  ş,ı,ğ gibi bazı türkçe karakterleri göstermiyor.

 

Aşşağıda

1-text kodlama alınıyor: windows1252
2-utf8 e dosya dönüştürülüyor.
3-dosyayı satır satır loga yazdırıyoruz.




String dosyaadi="abc.de";
String yol2 = Environment.getExternalStorageDirectory().toString()+"/"+"test"+"/";
String yol3=yol2+dosyaadi;
				
byte[] buf = new byte[4096];
String fileName = yol3;
java.io.FileInputStream fis = null;
				try {
				fis = new java.io.FileInputStream(fileName);
				} catch (FileNotFoundException e2) {e2.printStackTrace();}

			    UniversalDetector detector = new UniversalDetector(null);
			    int nread;
			    try {
					while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
					   detector.handleData(buf, 0, nread);}
				} catch (IOException e2) {
					e2.printStackTrace();
				}

			    detector.dataEnd();
			    String encoding = detector.getDetectedCharset();
			     if (encoding != null) {
			       System.out.println("Detected encoding = " + encoding);
			     } else {
			      System.out.println("No encoding detected.");
			    }
			    detector.reset();
			
				
				  String filename = yol3;
			         File file = new File(filename);
			         String content = null;
                                  try {
				content = FileUtils.readFileToString(file, encoding);
					} catch (IOException e1) {
						e1.printStackTrace();
					}try {
						FileUtils.write(file, content, "UTF-8");
				} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
		 		
		 		try {
		 			File fileDir = new File(yol3);
		 				
		 			BufferedReader in = new BufferedReader(
		 			   new InputStreamReader(
		 	                      new FileInputStream(fileDir), "UTF-8"));
		 			        
		 			String str;
		 			      
		 			while ((str = in.readLine()) != null) {
		 			    System.out.println(str);
		 				
		 			}
		 			        
		 	                in.close();
		 		    } 
		 		    catch (UnsupportedEncodingException e) 
		 		    {
		 				System.out.println(e.getMessage());
		 		    } 
		 		    catch (IOException e) 
		 		    {
		 				System.out.println(e.getMessage());
		 		    }
		 		    catch (Exception e)
		 		    {
		 				System.out.println(e.getMessage());
		 		    }

 

214 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user yakup2014
yakup2014
22.10.2015 - 01:13

veriyi çekerken dosyanın içindeki yazıların dili neyse, onları https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

veri okumayı kodlama ismi ile çekilebilir. türkçe için Windows-1254 yazmak gerek.

Profile picture for user ulasocal
ulasocal
21.10.2015 - 11:39

String data = new String(buf , "Windows-1252");
byte[] yenidata = data.getBytes("UTF-8"); 

şeklinde dene istersen.