cancel
Showing results for 
Search instead for 
Did you mean: 

How to save text as txt file ?

Former Member
0 Kudos

Hi,All.

when I create a string = "this si my country" in webDynpro .

I want save this as a *.txt file . and save this in my computer or open this txt file at once, how to come out this funciton ?

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Doke,

You can simply create a button on click of which you can write the following code and test your application. It will be saved as text file.

String abc = "what ever you want to save to text file save it in the string format";

try

{

IWDCachedWebResource resource = WDWebResource.getWebResource

(

abc.getBytes("UTF-8"), WDWebResourceType.UNKNOWN

);

resource.setResourceName("Doke.txt");

IWDWindow window = wdComponentAPI.getWindowManager().createNonModalExternalWindow(

resource.getUrl(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()),

resource.getResourceName());

window.show();

}

catch(Exception ex)

{

wdComponentAPI.getMessageManager().reportException( new WDNonFatalException(ex), false );

}

Hope this will help you.

Regards

Sagar Ingalwar

Former Member
0 Kudos

Hi Sagar,

after my test, your method is right,

can come out save data in another IE page when click the button,

thank you very much!

Btw, before display the data in IE page, popup a msgbox window, name is "File Download",

only I check this window first, then the IE page dada appear.

can we let the popup window not appear ,and display the IE page direct ?

Thanks!

Answers (7)

Answers (7)

Former Member
0 Kudos

thanks!

Former Member
0 Kudos

Hi Doke,

Here is a simple demostration for your requirement. I had done this in my project, it saves your text entered to .txt file. Just follow the step by step procedure. It will solve your problem

1) create a single view with Text area,a button(convert) and a LinkTOUrl UI element.

2)In your context create two value attributes Doc and Url of String Type.

3)Write the following code on click of button. and set the refrence property of LinktoUrl equals to Url.

public void onActionConvert(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionConvert(ServerEvent)

final String getstring = wdContext.currentContextElement().getDoc();

try

{

final IWDCachedWebResource resource = WDWebResource.getWebResource

(

getstring.getBytes("UTF-8"), WDWebResourceType.TXT

);

resource.setResourceName("WordDoc.txt");

wdContext.currentContextElement().setUrl( resource.getAbsoluteURL() );

}

catch (final Exception ex)

{

wdComponentAPI.getMessageManager().reportException( new WDNonFatalException(ex), false );

}

//@@end

}

After this when you click on Button(convert) the text you entered in Text Area will be exported to

file "wordDoc.txt" and you can save it to your local desktop on clicking on linktoURL UI element. Revert me if you have any concerns.

Regards

Raghu

Former Member
0 Kudos

Dear Raghunandan & Aishwarya

I have test your said ,

but All failed,

I don't know the reason.If you really test success,

I hope you sent you source code to me if can.

my e-mail is: lwhbdong # crystal-csc.cn

Thanks!

Former Member
0 Kudos
Former Member
0 Kudos

Hi,

Please refer to the following link:

Former Member
0 Kudos

hi!

in my application i have executed a bapi and exported it in a excel file .you can save your text in text file by just changing the type as .txt.

i have created one button and in the action of that button i have wriiten following code

//give a file name

String fileName = "vishal" + ".xls";

IWDCachedWebResource cachedExcelResource = null;

try

{

//create file

java.io.File f = new java.io.File("output.xls");

//create workbook

WritableWorkbook workbook = Workbook.createWorkbook(f);

//provide font

WritableFont red = new WritableFont(WritableFont.ARIAL,

WritableFont.DEFAULT_POINT_SIZE,

WritableFont.BOLD,

false,

UnderlineStyle.SINGLE,

Colour.DARK_BLUE);

WritableCellFormat redFormat = new WritableCellFormat(red);

WritableFont blue = new WritableFont(WritableFont.ARIAL,

WritableFont.DEFAULT_POINT_SIZE,

WritableFont.NO_BOLD,

false,

UnderlineStyle.NO_UNDERLINE,

Colour.BLACK);

WritableCellFormat blueFormat = new WritableCellFormat(blue);

WritableSheet sheet = workbook.createSheet("First Sheet", 0);

WritableSheet sheet2 = workbook.createSheet("second Sheet", 1);

jxl.write.Label label = new jxl.write.Label(0, 0, "ID", redFormat);

sheet.addCell(label);

label = new jxl.write.Label(1,0,"No",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(2,0,"Arrival city",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(3,0,"Depart",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(4,0,"Depart. city",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(5,0,"Curr.",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(6, 0,"Airline",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(7,0,"Airport",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(8,0,"ISO",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(9,0,"Apt",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(10,0,"Airfare",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(11,0,"Arrival Date",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(12,0,"Arrival",redFormat);

sheet.addCell(label);

label = new jxl.write.Label(13,0,"Date",redFormat);

sheet.addCell(label);

for(int j=1;j<wdContext.nodeFlight_List().size();j++)

{

for(int i=0;i<13;)

{

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirlineid(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getConnectid(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCityto(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getDeptime().toString(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCityfrom(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCurr(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirline(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirportfr(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getAirportto().toString(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getCurr_Iso().toString(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getPrice().toString(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getArrdate().toString(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getArrtime().toString(),blueFormat);

sheet.addCell(label);

label = new jxl.write.Label(i++,j,wdContext.nodeFlight_List().getFlight_ListElementAt(i).getFlightdate().toString(),blueFormat);

sheet.addCell(label);

}

}

workbook.setColourRGB(Colour.LIME, 0xff, 0, 0);

workbook.write();

FileInputStream excelCSVFile = new FileInputStream(f);

cachedExcelResource =

getCachedWebResource(

excelCSVFile,

fileName,

WDWebResourceType.getWebResourceType(

"xls",

"application/ms-excel"));

wdContext.currentContextElement().setFileLocation(cachedExcelResource.getURL());

workbook.close();

}

catch (Exception ex)

{

ex.printStackTrace();

}

and then in a global area

//@@begin others

public com.sap.tc.webdynpro.services.sal.url.api.IWDCachedWebResource getCachedWebResource( java.io.InputStream file, java.lang.String name, com.sap.tc.webdynpro.services.sal.url.api.WDWebResourceType type )

{

//@@begin getCachedWebResource()

IWDCachedWebResource cachedWebResource = null;

if (file != null)

{

cachedWebResource = WDWebResource.getWebResource(file, type);

cachedWebResource.setResourceName(name);

}

return cachedWebResource;

}

//@@end

also make sure that you have the required jar file in your lib.

thanks

vishal

Former Member
0 Kudos

Hi,


try {
	   File f = new File("filename");
	   RandomAccessFile raf = new RandomAccessFile(f, "rw");
    
	   // Read a character
	   String ch = raf.readLine();
    
	   // Seek to end of file
	   raf.seek(f.length());
    
	   // Append to the end
	   raf.writeBytes("aString");
	   raf.close();
   } catch (IOException e) {
   }

Former Member
0 Kudos

Hi,


try
{
   String fileName = "FileName.txt";
   FileWriter fileWriter = new FileWriter( fileName );
   BufferedWriter bufferedWriter = new BufferedWriter( fileWriter );
			
   bufferedWriter.write(" this si my country");
   bufferedWriter.newLine();
   bufferedWriter.write(" Some String 2");

   bufferedWriter.close();
}
catch( IOException e )
{
   e.printStackTrace();
}

Edited by: Aishwarya Sharma on Oct 11, 2008 6:27 AM