在java中将文本文件转换为字符串

在java中将文本文件转换为字符串,java,android,Java,Android,我编写了一个代码,通过将文本文件转换为字符串并发送到web服务来发送文本文件。请有人告诉我其他可用的方法将字符串作为流发送到Web服务 public class MainActivity extends Activity { Button b1; String s; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS

我编写了一个代码,通过将文本文件转换为字符串并发送到web服务来发送文本文件。请有人告诉我其他可用的方法将字符串作为流发送到Web服务

public class MainActivity extends Activity {
    Button b1;
    String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                File upfile=new File("sdcard/text/testfile.txt");
                try {
                    FileInputStream fin=new FileInputStream(upfile);
                    byte[] buffer= new byte[(int)upfile.length()];
                    new DataInputStream(fin).readFully(buffer);
                    fin.close();
                    s=new String(buffer,"UTF-8");
                    System.out.print(buffer);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, s, 20).show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

使用此代码从文件中读取数据并将其转换为字符串,然后根据需要提前执行处理--


使用此代码从文件中读取数据并将其转换为字符串,然后根据需要提前执行处理--

试试这个

public void mReadJsonData() {
    try {
        File f = new File("sdcard/text/testfile.txt");
        FileInputStream is = new FileInputStream(f);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        String text = new String(buffer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
试试这个

public void mReadJsonData() {
    try {
        File f = new File("sdcard/text/testfile.txt");
        FileInputStream is = new FileInputStream(f);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        String text = new String(buffer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

你应该使用StringBuilder,而不是循环中的字符串连接。我正在获取一个文本文件并将其发送到web服务,因此可以这样发送值。文件编码是当前平台的编码;更好地使用,
InputStreamReader(FileInputStream,“UTF-8”)
。也
encoded+=行+“\n”左右,因为readLine会删除行的结尾。如果可能,最好使用Apache commons io。您应该使用StringBuilder,而不是循环内的字符串连接。我正在获取一个文本文件并将其发送到web服务,因此可以这样发送值文件编码是当前平台的编码;更好地使用,
InputStreamReader(FileInputStream,“UTF-8”)
。也
encoded+=行+“\n”左右,因为readLine会删除行的结尾。如果可能,更好地使用Apache commons io。