How to export a txt file using Java

Source: Internet
Author: User

How to export a txt file using Java

This example describes how to export a txt file in Java. Share it with you for your reference. The details are as follows:

Example 1

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

/**

* Export File export

*/

@ RequestMapping (value = "/grab/export/csv", method = {RequestMethod. GET })

Public void exportCsv (HttpServletRequest request, HttpServletResponse response ){

String userId = ServletRequestUtils. getStringParameter (request, "userId", "test ");

ModelAndView mav = new ModelAndView ();

SqlVideoList sqlVideoList = new SqlVideoList ();

List <VideoListModel> list = new ArrayList <VideoListModel> ();

Try {

List = sqlVideoList. selectSuccessDate (userId );

} Catch (SQLException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

}

// Export the txt file

Response. setContentType ("text/plain ");

String fileName = "videolist ";

Try {

FileName = URLEncoder. encode ("videolist", "UTF-8 ");

} Catch (UnsupportedEncodingException e1 ){

// TODO Auto-generated catch block

E1.printStackTrace ();

}

Response. setHeader ("Content-Disposition", "attachment; filename =" + fileName + ". txt ");

BufferedOutputStream buff = null;

StringBuffer write = new StringBuffer ();

String enter = "\ r \ n ";

ServletOutputStream outSTr = null;

Try {

OutSTr = response. getOutputStream (); // create

Buff = new BufferedOutputStream (outSTr );

// Write the content to the file

If (list. size ()> 0 ){

For (int I = 0; I <list. size (); I ++ ){

Write. append (list. get (I). getUrl () + ",");

Write. append (list. get (I). getTitle ());

Write. append (enter );

}

}

Buff. write (write. toString (). getBytes ("UTF-8 "));

Buff. flush ();

Buff. close ();

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

Try {

Buff. close ();

OutSTr. close ();

} Catch (Exception e ){

E. printStackTrace ();

}

}

}

Example 2:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

/**

* Export the VIP redemption code.

* @ Throws UnsupportedEncodingException

*/

@ RequestMapping (value = "/{exchangeId}/{packageId}/export", method = RequestMethod. GET)

Public void writeToTxt (@ PathVariable String exchangeId, @ PathVariable String packageId, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

String schoolId = this. getSchoolId (request); // network school ID

// Obtain the redemption code for the VIP package of the online school.

VipCodeExample example = new VipCodeExample ();

Example. createCriteria (). andSchoolIdEqualTo (schoolId). andPackageIdEqualTo (packageId). andExchangeIdEqual

To (exchangeId );

List <VipCode> vipCodes = vipExchangeManager. getVipCode (example );

If (vipCodes. size ()> 0 ){

Response. setContentType ("text/plain"); // The key settings of the two lines

Response. addHeader ("Content-Disposition ",

"Attachment; filename =" + java.net. URLEncoder. encode (vipCodes. get (0). getName (), "UTF-8") + ". txt ");

// Filename specifies the default name

VipCode vipcode = new VipCode ();

BufferedOutputStream buff = null;

StringBuffer write = new StringBuffer ();

String tab = "";

String enter = "\ r \ n ";

ServletOutputStream outSTr = null;

Try {

OutSTr = response. getOutputStream (); // create

Buff = new BufferedOutputStream (outSTr );

For (int I = 0; I <vipCodes. size (); I ++ ){

Vipcode = vipCodes. get (I );

Write. append (I + 1); // serial number

Write. append (tab );

Write. append (vipcode. getExchangeCode ());

Write. append (tab );

If ("normal". equals (vipcode. getStatus ())){

Write. append ("normal ");

} Else {

Write. append ("Redeemed ");

}

Write. append (enter );

}

Buff. write (write. toString (). getBytes ("UTF-8 "));

Buff. flush ();

Buff. close ();

} Catch (Exception e ){

E. printStackTrace ();

} Finally {

Try {

Buff. close ();

OutSTr. close ();

} Catch (Exception e ){

E. printStackTrace ();

}

}

}

}

I hope this article will help you with java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.