为什么记事本不能在字符串中显示新的行字符?

时间:2013-01-07 08:12:33

标签: android

我在Word pad(文本后面的新行)中获得了完美的结果但是我无法使用以下代码在记事本中获得新行。并且希望得到与填充文字相同的结果。

 private void getIntentDetails() {

        intent = getIntent();

        if (intent.hasExtra("testName")) {
            testName = (String) intent.getExtras().get("testName");
            Log.e("testName ", "testName " + testName);
            appendLog("TestName : ");
            appendLog(testName);
            appendLog(",  ");
            //appendLog("\n");
        }

        if (intent.hasExtra("testDate")) {
            testDate = (String) intent.getExtras().get("testDate");
            Log.e("testDate ", "testDate " + testDate);
            appendLog("TestDate : ");
            appendLog(testDate);
            appendLog(",  ");
        }

        if (intent.hasExtra("testTime")) {
            testTime = (String) intent.getExtras().get("testTime");
            Log.e("testTime ", "testTime " + testTime);
            appendLog("TestTime : ");
            appendLog(testTime);
            appendLog(",  ");
        }

        if (intent.hasExtra("savedName")) {
            savedName = (String) intent.getExtras().get("savedName");
            Log.e("savedName ", "savedName " + savedName);
            appendLog("SavedName : ");
            appendLog(savedName);
            appendLog(",  ");
        }


        if (intent.hasExtra("sensitivity")) {
            sensitivity = (String) intent.getExtras().get("sensitivity");
            Log.e("sensitivity ", "sensitivity " + sensitivity);
            appendLog("Accelerometer Sensitivity : ");
            appendLog(sensitivity);
            appendLog(" ,  ");
        }

        if (intent.hasExtra("xvalues")) {
            xValues = (ArrayList<Float>) intent.getExtras().get("xvalues");
            Log.e("xValues ", "xValues " + xValues.size());
            for (int indexx = 0; indexx < xValues.size(); indexx++) {
                xvalue = xValues.get(indexx).toString();
                if (indexx == 0) {
                    appendLog("X-Values : ");
                }
                appendLog(xvalue);
                appendLog(", ");

            }

        }
        Log.d("X-Values : ", ""+ xValues.size());

        if (intent.hasExtra("yvalues")) {
            yValues = (ArrayList<Float>) intent.getExtras().get("yvalues");
            Log.e("yValues ", "yValues " + yValues.size());
            for (int indexy = 0; indexy < yValues.size(); indexy++) {
                yvalue = yValues.get(indexy).toString();
                if (indexy == 0) {
                    appendLog("Z-Values : ");
                }
                appendLog(yvalue);
                appendLog(", ");

            }

        }
        Log.e("Z-Values : ", ""+ yValues.size());

        if(intent.hasExtra("xpos")){
            xPos = (String[]) intent.getExtras().get("xpos");


        }
//      for(int m = 0; m< xPos.length;m++){
//          Log.d("xPos", "  "+ xPos[m]);
//      }
        if(intent.hasExtra("xneg")){
        xNeg = (String[]) intent.getExtras().get("xneg");
        Log.e("xNeg ", "xNeg " + xNeg.length);

        }
//      for(int m = 0; m< xNeg.length;m++){
//          Log.d("xNeg", "  "+ xNeg[m]);
//      }
        if(intent.hasExtra("ypos")){
            yPos = (String[]) intent.getExtras().get("ypos");
            Log.e("yPos ", "yPos " + yPos.length);

        }
//      for(int m = 0; m< yPos.length;m++){
//          Log.d("yPos", "  "+ yPos[m]);
//      }
        if(intent.hasExtra("yneg")){
            yNeg = (String[]) intent.getExtras().get("yneg");
            Log.e("yNeg ", "yNeg " + yNeg.length);

        }
//      for(int m = 0; m< yNeg.length;m++){
//          Log.d("yNeg", "  "+ yNeg[m]);
//      }

    }

    private void appendLog(String text) {

        File logFile = new File(fileLocation);
        if (!logFile.exists()) {
            try {
                Log.e("file was not created", "file  created");
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            // BufferedWriter for performance, true to set append to file flag
            BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)/*,8192*/);
            //buf.newLine();
        //  System.getProperty("line.separator");
            buf.write(text);
            //System.getProperty("line.separator");
            //buf.append("\n");
            //buf.write("/\n");
            buf.newLine();

            buf.close();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:0)

这是因为记事本不是富文本阅读器,它只能理解

中不是\的字符

答案 1 :(得分:0)

我是使用FileOutputStream完成的。

以下代码对我有用......

try{
FileOutputStream fop = new FileOutputStream(logFile,true);
// get the content in bytes
byte[] contentInBytes = text.getBytes();

fop.write(contentInBytes);
//writing new line using bytes
String br = "\r\n";
fop.write (br.getBytes ());
fop.flush();
fop.close();
}catch (Exception e) {
e.printStackTrace();
}