Hi,
This is my activity. I am just trying to write data to a file and again 
reading the same file as a RandomAccessFile.
Some time the code is working fine but some time it is showing
----- [ D/SntpClient(61): request time failed: java.net.SocketException: 
Address family not supported by protocol].---
Plz help me. Thanks in advance.

This is the code  
----------------------------------------------
package nihar.rajesh.records;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.net.SocketException;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

@SuppressWarnings("serial")
public class StudentsOutput extends Activity implements Serializable{
 TextView roll,name,addr,mob,email;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studentoutput);
        
        roll = (TextView)findViewById(R.id.textRollno);
        name = (TextView)findViewById(R.id.textName);
        addr = (TextView)findViewById(R.id.textAddr);
        mob = (TextView)findViewById(R.id.textMob);
        email = (TextView)findViewById(R.id.textEmail);
        
        StudentRecord stdrecord= new StudentRecord();
        
        stdrecord =(StudentRecord) 
getIntent().getExtras().get("StudentDetails");
        
        
       // Toast.makeText(this, stdrecord.roll, Toast.LENGTH_SHORT).show();
        
        roll.setText("Roll No:  "+ stdrecord.roll);
        name.setText("Name:  "+stdrecord.name);
        addr.setText("Address:  "+stdrecord.addr);
        mob.setText("Mob:  "+stdrecord.mob);
        email.setText("Email:  "+stdrecord.email);
        
       
       //Creating the file 
        String filename = "Student.txt";
File file = new File(Environment.getExternalStorageDirectory(),
filename);
FileOutputStream fos;
 //creating the byte array
 if( stdrecord.toSring().length()!= 80)
   {
   StringBuilder tmp= new StringBuilder();
   tmp.append(stdrecord.toSring());
   int len=stdrecord.toString().length();
               System.out.print(len);
   for (int i=0;i<(80-len); i++)
   {
   tmp.append(" ");
   }
 // Creating the byte array   
      byte[] data = tmp.toString().getBytes();
   
      try {
       fos = new FileOutputStream(file,false);
       fos.write(data);
           fos.flush();
           fos.close();
          } 
     catch (FileNotFoundException e)
        {
       // handle exception
        }
     catch (IOException e)
        {
      // handle exception
        }
 }     
       
/*   try{
  // Create file 
  FileWriter fstream = new FileWriter("Student.txt",true);
  BufferedWriter out = new BufferedWriter(fstream);
  out.write("Hello Java");
  //Close the output stream
  out.close();
  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }*/
 // Reading the file in Random access form
RandomAccessFile access=null;
try 
 {
   access = new RandomAccessFile(file,"r");
   System.out.println("FilePinter:"+access.getFilePointer());
           System.out.println("Read full line: " + access.readLine());
           System.out.println("FilePinter:"+access.getFilePointer());
 }
 catch (FileNotFoundException e)
 {
    // TODO Auto-generated catch block
e.printStackTrace();
 }
catch(SocketException ex)
       {
         Log.e("Error : " , "Error on soapPrimitiveData() " + 
ex.getMessage());
           ex.printStackTrace();
       }
catch (IOException e)
 {
// TODO Auto-generated catch block
e.printStackTrace();
 }
 finally
{
try {
access.close();
    }
catch (IOException e)
    {
// TODO Auto-generated catch block
e.printStackTrace();
    }
}
}}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to