Home Java Javagetting Started How to save the string data in the arraylist collection into a text file

How to save the string data in the arraylist collection into a text file

Sep 17, 2020 pm 05:13 PM
arraylist gather

How to save the string data in the arraylist collection into a text file

Analysis:

(Recommended tutorial: java course)

We can know the following through the meaning of the question:

  1. The ArrayList collection stores strings

  2. Traverse the ArrayList collection and obtain the data

  3. Then store it in a text file

  4. Text file description using text file

  5. Data source: ArrayList ---- Traverse each A string data

  6. Destination: a.txt---FileWriter --- BufferdReater

Code implementation:

package cn.itcast_02;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
 * 需求:把ArrayList集合的字符串数据存储到文本文件中
 * 
 * 分析: 
 *     通过题目的意思我们可以知道如下内容
 *     ArrayList集合里存储的是字符串
 *     遍历ArrayList集合,把数据获取
 *     然后存储到文本文文件中
 *     文本文文件说明使用文本文件
 *     
 *     数据源:ArrayList<String> ---- 遍历每一个字符串数据
 *     
 *     目的地:a.txt---FileWriter --- BufferdReater
 * 
 */
public class 把集合中的数据存储到文件 {
         public static void main(String[] args) throws IOException {
				//封装数据与创建对象
	        	 ArrayList<String> array = new ArrayList<String>();
	        	 array.add("hello");
	        	 array.add("world");
	        	 array.add("java");
	        	 
	        	 //封装目的地
	        	 BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
	        	 
	        	 
	        	 //遍历集合
	        	 for(String s : array) {
	        		bw.write(s); 
	        		bw.newLine();
	        		bw.flush();
	        	 }
	        	 
	        	 //释放资源
	        	 bw.close();
        	 }
}
Copy after login

Related recommendations: java introductory tutorial

The above is the detailed content of How to save the string data in the arraylist collection into a text file. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

Why is it difficult to implement collection-like functions in Go language? Why is it difficult to implement collection-like functions in Go language? Mar 24, 2024 am 11:57 AM

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages ​​such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

How to optimize Java collection sorting performance How to optimize Java collection sorting performance Jun 30, 2023 am 10:43 AM

Java is a powerful programming language that is widely used in various types of software development. In Java development, scenarios that often involve sorting collections are involved. However, if performance optimization is not performed for collection sorting, the execution efficiency of the program may decrease. This article will explore how to optimize the performance of Java collection sorting. 1. Choose the appropriate collection class In Java, there are many collection classes that can be used for sorting, such as ArrayList, LinkedList, TreeSet, etc. Different collection classes are in

How to check if ArrayList contains a certain element in Java? How to check if ArrayList contains a certain element in Java? Sep 03, 2023 pm 04:09 PM

You can use the contains() method of the List interface to check whether an object exists in the list. contains() method booleancontains(Objecto) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null?e==null:o.equals(e)). Parameter c - the element whose presence in this list is to be tested. Return Value Returns true if this list contains the specified element. Throws ClassCastException - if the specified element's type is incompatible with this list (optional). NullP

Remove elements from ArrayList using java's ArrayList.remove() function Remove elements from ArrayList using java's ArrayList.remove() function Jul 24, 2023 pm 01:21 PM

Use java's ArrayList.remove() function to remove elements from an ArrayList. In Java, ArrayList is a commonly used collection class used to store and operate a set of elements. The ArrayList class provides many methods to add, delete, modify, and query elements in the collection. One of the more frequently used methods is remove(), which can remove elements from an ArrayList. The remove() method of ArrayList has two overloaded forms: one

A Practical Guide to the Where Method in Laravel Collections A Practical Guide to the Where Method in Laravel Collections Mar 10, 2024 pm 04:36 PM

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

What is the reason why the initial capacity of ArrayList in Java is 10? What is the reason why the initial capacity of ArrayList in Java is 10? May 10, 2023 pm 02:19 PM

Why is the initial capacity of HashMap 16? When talking about the initialization capacity of ArrayList, we must first review the initialization capacity of HashMap. Taking the Java8 source code as an example, there are two relevant factors in HashMap: initialization capacity and loading factor: /***Thedefaultinitialcapacity-MUSTbeapoweroftwo.*/staticfinalintDEFAULT_INITIAL_CAPACITY=1>1);if(newCapacity-minCapacity0)newCapacity=hugeCapacity

Common concurrent collections and thread safety issues in C# Common concurrent collections and thread safety issues in C# Oct 09, 2023 pm 10:49 PM

Common concurrent collections and thread safety issues in C# In C# programming, handling concurrent operations is a very common requirement. Thread safety issues arise when multiple threads access and modify the same data at the same time. In order to solve this problem, C# provides some concurrent collection and thread safety mechanisms. This article will introduce common concurrent collections in C# and how to deal with thread safety issues, and give specific code examples. Concurrent collection 1.1ConcurrentDictionaryConcurrentDictio

See all articles