


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:
The ArrayList collection stores strings
Traverse the ArrayList collection and obtain the data
Then store it in a text file
Text file description using text file
Data source: ArrayList
---- Traverse each A string data 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(); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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# 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
