How to read word from file

In this article, we are going to learn How to Read text File word by word in C. We will read each word from the text file in each iteration.

C fscanf Function


The fscanf() function is available in the C library. This function is used to read formatted input from a stream. The syntax of the fscanf function is:

Syntax

int fscanf(FILE *stream, const char *format, ...)

Parameters :

  • stream − This is the pointer to a FILE object that identifies the stream.
  • format − This is the C string that contains one or more of the following items − Whitespace character, Non-whitespace character, and Format specifiers.
  • A format specifier will be as [=%[*][width][modifiers]type=].

1. Read File Word by Word in C using fscanf Function


Here we are making use of the fscanf function to read the text file. The first thing we are going to do is open the file in reading mode. So using fopen() function and “r” read mode we opened the file. The next step is to find the file stats like what is the size of the data this file contains. so we can allocate exact memory for the buffer that is going to hold the content of this file. We are using the stat() function to find the file size.

  • Once we have the size and buffer allocated for this size, we start reading the file by using the fscanf() function.
  • We keep reading the file word by word until we reach the end of file.In fscanf function, we are passing “%39[^-n] as the argument so we can read the text until we find the next word.
  • The code will look like this:
fscanf(in_file, "%39[^-n]", file_contents)

C Program to Read text File word by word


To run this program, we need one text file with the name Readme.txt in the same folder where we have our code.The content of the file is:

Hello My name is 
John 
danny
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

const char* filename = "Readme.txt";

int main(int argc, char *argv[])
{
    FILE *in_file = fopen(filename, "r");
    if (!in_file) 
	{
        perror("fopen");
        return 0;
    }

    struct stat sb;
    if (stat(filename, &sb) == -1) 
	{
        perror("stat");
        return 0;
    }

    char *file_contents = malloc(sb.st_size);

    while (fscanf(in_file, "%[^-n ] ", file_contents) != EOF) {
      printf("> %sn", file_contents);
	  }
    
	

    fclose(in_file);
    return 0;
}

Output

Hello
My
name
is
John
danny

Like the title says, im trying to write a program that can read individual words from a text file and store them to String variables. I know how to use a FileReader or FileInputStream to read a single char but for what I’m trying to this wont work. Once I input the words I am trying to compare these with other String variables in my program using .equals so it would be best if I can import as Strings. I am also okay with inputting an entire line from a text file as a String in which case Ill just put one word on each line of my file. How do I input words from a text file and store them to String variables?

EDIT:
Okay, that duplicate sort of helps. It might work for me but the reason my question is a little different is because the duplicate only tells how to read a single line. Im trying to read the individual words in the line. So basically splitting the line String.

asked Jul 12, 2015 at 18:06

Ashwin Gupta's user avatar

Ashwin GuptaAshwin Gupta

2,1589 gold badges30 silver badges60 bronze badges

3

These are all really complex answers. And I am sure they are all useful. But I prefer the elegantly simple Scanner :

public static void main(String[] args) throws Exception{
    Scanner sc = new Scanner(new File("fileName.txt"));
    while(sc.hasNext()){
        String s = sc.next();
        //.....
    }
}

answered Jul 12, 2015 at 18:24

Misha's user avatar

MishaMisha

1901 silver badge9 bronze badges

1

To read lines from a text file, you can use this (uses try-with-resources):

String line;

try (
    InputStream fis = new FileInputStream("the_file_name");
    InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8"));
    BufferedReader br = new BufferedReader(isr);
) {
    while ((line = br.readLine()) != null) {
        // Do your thing with line
    }
}

More compact, less-readable version of the same thing:

String line;

try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("the_file_name"), Charset.forName("UTF-8")))) {
    while ((line = br.readLine()) != null) {
        // Do your thing with line
    }
}

To chunk a line into individual words, you can use String.split:

while ((line = br.readLine()) != null) {
    String[] words = line.split(" ");
    // Now you have a String array containing each word in the current line
}

answered Jul 12, 2015 at 18:14

spork's user avatar

sporkspork

1,1751 gold badge11 silver badges16 bronze badges

1

You must use StringTokenizer! here an example and read this String Tokenizer

private BufferedReader innerReader; 
public void loadFile(Reader reader)
        throws IOException {
    if(reader == null)
    {
        throw new IllegalArgumentException("Reader not valid!");
    }
        this.innerReader = new BufferedReader(reader);
    String line;
    try
    {
    while((line = innerReader.readLine()) != null)
    {
        if (line == null || line.trim().isEmpty())
            throw new IllegalArgumentException(
                    "line empty");
        //StringTokenizer use delimiter for split string
        StringTokenizer tokenizer = new StringTokenizer(line, ","); //delimiter is ","
        if (tokenizer.countTokens() < 4)
            throw new IllegalArgumentException(
                    "Token number not valid (<= 4)");
        //You can change the delimiter if necessary, string example
        /*
        Hello / bye , hi
        */
        //reads up "/"
        String hello = tokenizer.nextToken("/").trim();
        //reads up ","
        String bye = tokenizer.nextToken(",").trim();
        //reads up to end of line
        String hi = tokenizer.nextToken("nr").trim();
        //if you have to read but do not know if there will be a next token do this
        while(tokenizer.hasMoreTokens())
        {
          String mayBe = tokenizer.nextToken(".");
        }
    }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

answered Jul 12, 2015 at 18:13

Michele Lacorte's user avatar

Michele LacorteMichele Lacorte

5,2637 gold badges32 silver badges53 bronze badges

3

In java8 you can do something like the following:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Foo {
    public List<String> readFileIntoListOfWords() {
        try {
            return Files.readAllLines(Paths.get("somefile.txt"))
                .stream()
                .map(l -> l.split(" "))
                .flatMap(Arrays::stream)
                .collect(Collectors.toList());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}

Though I suspect that the argument to split may need to be changed, eg to trim punctuation from the end of a word

answered Jul 12, 2015 at 18:31

beresfordt's user avatar

beresfordtberesfordt

5,06810 gold badges34 silver badges43 bronze badges

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    Like Article

    Prerequisites: File Handling in Python Given a text file and the task is to read the information from file word by word in Python. Examples:

    Input: I am R2J! Output: I am R2J! Input: Geeks 4 Geeks And in that dream, we were flying. Output: Geeks 4 Geeks And in that dream, we were flying.

    Approach:

    1. Open a file in read mode which contains a string.
    2. Use for loop to read each line from the text file.
    3. Again use for loop to read each word from the line splitted by ‘ ‘.
    4. Display each word from each line in the text file.

    Example 1: Let’s suppose the text file looks like this – Text File: read-word-by-word-python 

    Python3

    with open('GFG.txt','r') as file:

        for line in file:

            for word in line.split():

                print(word)

    Output:

    Geeks
    4
    geeks

    Time Complexity: O(n), where n is the total number of words in the file.
    Auxiliary Space: O(1), as the program is reading and displaying each word one by one without storing any additional data.

    Example 2: Let’s suppose the text file contains more than one line. Text file: python-read-word-by-word 

    Python3

    with open('GFG.txt','r') as file:

        for line in file:

            for word in line.split():

                print(word)

    Output:

    Geeks
    4
    Geeks
    And
    in
    that
    dream,
    we
    were
    flying.

    Time complexity: O(n), where n is the total number of words in the file.
    Auxiliary space: O(1), as only a constant amount of extra space is used to store each word temporarily while printing it.

    Like Article

    Save Article

    python read word document

    This post will talk about how to read Word Documents with Python. We’re going to cover three different packages – docx2txt, docx, and my personal favorite: docx2python.

    The docx2txt package

    Let’s talk about docx2text first. This is a Python package that allows you to scrape text and images from Word Documents. The example below reads in a Word Document containing the Zen of Python. As you can see, once we’ve imported docx2txt, all we need is one line of code to read in the text from the Word Document. We can read in the document using a method in the package called process, which takes the name of the file as input. Regular text, listed items, hyperlink text, and table text will all be returned in a single string.

    import docx2txt
    
    # read in word file
    result = docx2txt.process("zen_of_python.docx")
    
    

    python scrape word document

    What if the file has images? In that case we just need a minor tweak to our code. When we run the process method, we can pass an extra parameter that specifies the name of an output directory. Running docx2txt.process will extract any images in the Word Document and save them into this specified folder. The text from the file will still also be extracted and stored in the result variable.

    import docx2txt
    
    result = docx2txt.process("zen_of_python_with_image.docx", "C:/path/to/store/files")
    
    

    Sample Image

    python scrape image from word document

    docx2txt will also scrape any text from tables. Again, this will be returned into a single string with any other text found in the document, which means this text can more difficult to parse. Later in this post we’ll talk about docx2python, which allows you to scrape tables in a more structured format.

    The docx package

    The source code behind docx2txt is derived from code in the docx package, which can also be used to scrape Word Documents. docx is a powerful library for manipulating and creating Word Documents, but can also (with some restrictions) read in text from Word files.

    In the example below, we open a connection to our sample word file using the docx.Document method. Here we just input the name of the file we want to connect to. Then, we can scrape the text from each paragraph in the file using a list comprehension in conjunction with doc.paragraphs. This will include scraping separate lines defined in the Word Document for listed items. Unlike docx2txt, docx, cannot scrape images from Word Documents. Also, docx will not scrape out hyperlinks and text in tables defined in the Word Document.

    import docx
    
    # open connection to Word Document
    doc = docx.Document("zen_of_python.docx")
    
    # read in each paragraph in file
    result = [p.text for p in doc.paragraphs]
    
    

    python docx

    The docx2python package

    docx2python is another package we can use to scrape Word Documents. It has some additional features beyond docx2txt and docx. For example, it is able to return the text scraped from a document in a more structured format. Let’s test out our Word Document with docx2python. We’re going to add a simple table in the document so that we can extract that as well (see below).

    python word document table

    docx2python contains a method with the same name. If we call this method with the document’s name as input, we get back an object with several attributes.

    from docx2python import docx2python
    
    # extract docx content
    doc_result = docx2python('zen_of_python.docx')
    
    

    Each attribute provides either text or information from the file. For example, consider that our file has three main components – the text containing the Zen of Python, a table, and an image. If we call doc_result.body, each of these components will be returned as separate items in a list.

    # get separate components of the document
    doc_result.body
    
    # get the text from Zen of Python
    doc_result[0]
    
    # get the image
    doc_result[1] 
    
    # get the table text
    doc_result[2]
    
    

    Scraping a word document table with docx2python

    The table text result is returned as a nested list, as you can see below. Each row (including the header) gets returned as a separate sub-list. The 0th element of the list refers to the header – or 0th row of the table. The next element refers to the next row in the table and so on. In turn, each value in a row is returned as an individual sub-list within that row’s corresponding list.

    docx2python scrape table

    We can convert this result into a tabular format using pandas. The data frame is still a little messy – each cell in the data frame is a list containing a single value. This value also has quite a few “t”‘s (which represent tab spaces).

    pd.DataFrame(doc_result.body[1][1:])
    
    

    python scrape table from word file

    Here, we use the applymap method to apply the lambda function below to every cell in the data frame. This function gets the individual value within the list in each cell and removes all instances of “t”.

    import pandas as pd
    
    
    pd.DataFrame(doc_result.body[1][1:]).
                                applymap(lambda val: val[0].strip("t"))
    
    
    

    docx2python pandas data frame

    Next, let’s change the column headers to what we see in the Word file (which was also returned to us in doc_result.body).

    
    df.columns = [val[0].strip("t") for val in doc_result.body[1][0]]
    
    
    

    docx2python scrape table from word document

    Extracting images

    We can extract the Word file’s images using the images attribute of our doc_result object. doc_result.images consists of a dictionary where the keys are the names of the image files (not automatically written to disk) and the corresponding values are the images files in binary format.

    type(doc_result.images) # dict
    
    doc_result.images.keys() # dict_keys(['image1.png'])
    
    

    We can write the binary-formatted image out to a physical file like this:

    
    for key,val in doc_result.images.items():
        f = open(key, "wb")
        f.write(val)
        f.close()
    
    

    Above we’re just looping through the keys (image file names) and values (binary images) in the dictionary and writing each out to file. In this case, we only have one image in the document, so we just get one written out.

    Other attributes

    The docx2python result has several other attributes we can use to extract text or information from the file. For example, if we want to just get all of the file’s text in a single string (similar to docx2txt) we can run doc_result.text.

    # get all text in a single string
    doc_result.text
    
    

    In addition to text, we can also get metadata about the file using the properties attribute. This returns information such as the creator of the document, the created / last modified dates, and number of revisions.

    doc_result.properties
    
    

    If the document you’re scraping has headers and footers, you can also scrape those out like this (note the singular version of “header” and “footer”):

    # get the headers
    doc_result.header
    
    # get the footers
    doc_result.footer
    
    

    Footnotes can also be extracted like this:

    doc_result.footnotes
    
    

    Getting HTML returned with docx2python

    We can also specify that we want to get an HTML object returned with the docx2python method that supports a few types of tags including font (size and color), italics, bold, and underline text. We just need to specify the parameter “html = True”. In the example below we see The Zen of Python in bold and underlined print. Corresponding to this, we can see the HTML version of this in the second snapshot below. The HTML feature does not currently support table-related tags, so I would recommend using the method we went through above if you’re looking to scrape tables from Word documents.

    
    doc_html_result = docx2python('zen_of_python.docx', html = True)
    
    
    

    python word document html

    python get html from word document

    Hope you enjoyed this post! Please check out other Python posts of mine below or by clicking here.

    This tutorial explains how to read a text file and search any specific word in java. In order to search any specific word from a text file, we need to first read text file. Here we have provided simple example for reading a text file and search any specific word from that.

    How to read a text file and search any specific word in java

    sample.txt
    we are going to use below text file content using java code.

    Simple Form Validation In Reactjs Example
    Create a Dropdown Menu using ReactJS
    Installing React Native on Windows Tutorial
    Create Dropdown Menu In React Native
    How To Customize Button In React Native
    Facebook loading animation using CSS3
    Facebook Style Chat Application with jQuery and CSS
    Append Or Prepend HTML Using ReactJS
    Create Ripple Animation Effect Using Css
    Ripple Effect Animation On Button Click With CSS3

    FileWordSearch.txt

    package demo;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class FileWordSearch {
    
     public static void main(String[] args) throws IOException {
      // TODO Auto-generated method stub
    
      String[] words = null; // Intialize the word Array
    
      // Creation of File Descriptor for input file
      File f1 = new File("sample.txt");
    
      // Creation of File Reader object
      FileReader fr = new FileReader(f1);
    
      // Creation of BufferedReader object
      BufferedReader br = new BufferedReader(fr);
    
      String s;
      String input = "Facebook"; // Input word to be searched
      int count = 0; // Intialize the word to zero
    
      // Reading Content from the file
      while ((s = br.readLine()) != null) {
       words = s.split(" "); // Split the word using space
       for (String word : words) {
        if (word.equals(input)) { // Search for the given word
         count++; // If Present increase the count by one
        }
       }
      }
      if (count != 0) { // Check for count not equal to zero
       System.out.println("The given word is present for " + count
         + " Times in the file");
      } else {
       System.out.println("The given word is not present in the file");
      }
    
      fr.close();
    
     }
    }

    Output:-
    ——————

    This is all about Java Program to Search for a given word in a File. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

    This post will discuss how to read all text from a file into a string in Java.

    There are several ways to read the contents of a file into a string using Plain Java and third-party libraries such as Guava, Apache Commons IO, etc. Also, with the introduction of the Files class in Java 7, several static methods are included with each subsequent release that operate on files, directories, or other types of files. All these are discussed below in detail:

    1. Using Java 7 (java.nio.file.Files.readAllBytes)

    To read all the bytes from a file, we can use the readAllBytes() method, which takes the path to the file and returns a byte array containing the bytes read from the file. To get output in the string format, pass the byte array to the String constructor with a charset for decoding.

    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

    import java.io.File;

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    import java.nio.file.Files;

    import java.nio.file.Paths;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException

        {

            byte[] encoded = Files.readAllBytes(Paths.get(path));

            return new String(encoded, encoding);

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    2. Using Java 7 (java.nio.file.Files.readAllLines)

    To read the contents of a file into a string, we can use the readAllLines() method, which takes the file’s path. It is overloaded to additionally accept the charset to be used for decoding.

    This method is convenient to read all lines in a single operation but returns a list of strings and strips line terminators from the end of each line. We can easily handle this by joining each of the elements of the list with a line separator, as shown below:

    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

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    import java.nio.file.Files;

    import java.nio.file.Paths;

    import java.util.List;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException

        {

            List<String> lines = Files.readAllLines(Paths.get(path), encoding);

            return String.join(System.lineSeparator(), lines);

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    3. Using Java 8 (java.nio.file.Files.lines)

    Java 8 introduced Files.lines() method, which can read all lines from a file as a stream. It takes the path to the file and overloaded it to accept the charset for decoding.

    Like Files.readAllLines(), Files.lines() method doesn’t include line-termination characters, which can be handled explicitly, as shown below:

    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

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    import java.nio.file.Files;

    import java.nio.file.Paths;

    import java.util.stream.Collectors;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException {

            String content = Files.lines(Paths.get(path), encoding)

                                .collect(Collectors.joining(System.lineSeparator()));

            return content;

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    4. Using Java 8 (java.io.BufferedReader.lines)

    Java 8 also introduced BufferedReader.lines() method, which returns a stream of lines of text read from BufferedReader. This is demonstrated below using Stream:

    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

    import java.io.BufferedReader;

    import java.io.File;

    import java.io.FileReader;

    import java.io.IOException;

    import java.util.stream.Collectors;

    class Main

    {

        public static String readFile(String path) throws IOException {

            BufferedReader reader = new BufferedReader(new FileReader(new File(path)));

            return reader.lines().collect(Collectors.joining(System.lineSeparator()));

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    5. Using Java 11 (java.nio.file.Files.readString)

    Java 11 introduced the Files.readString() method, which can read all characters from a file into a string. It takes a path to the file and overloaded to accept the charset used for decoding from bytes to characters.

    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

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    import java.nio.file.Files;

    import java.nio.file.Paths;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException {

            return Files.readString(Paths.get(path), encoding);

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    6. Using Guava’s Files.asCharSource()

    Several third-party libraries provide utility methods for working with files. Guava provides the Files.asCharSource() method, which can be used to read file contents into a string using the given character set, as shown below:

    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

    import com.google.common.base.Charsets;

    import com.google.common.io.Files;

    import java.io.File;

    import java.io.IOException;

    import java.nio.charset.Charset;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException {

            return Files.asCharSource(new File(path), encoding).read();

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, Charsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    7. Using Guava’s Files.readLines()

    If you prefer Google’s Guava library, you can use the Files.readLines(file, charset) method reads all the lines from a file and returns a mutable List. For an ImmutableList, use Files.asCharSource(file, charset).readLines(). Note that both these methods strip line-termination characters from the end of each line.

    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

    import com.google.common.base.Charsets;

    import com.google.common.base.Joiner;

    import com.google.common.io.Files;

    import java.io.File;

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.util.List;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException {

            List<String> lines = Files.readLines(new File(path), encoding);

            return Joiner.on(System.lineSeparator()).join(lines);

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, Charsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    8. Using Apache Commons IO

    Like Guava library, Apache Commons IO FileUtils class provides the readFileToString() method that allows you to read the contents of any file into a string using the specified charset for converting Bytes from the file into characters.

    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

    import org.apache.commons.io.FileUtils;

    import java.io.File;

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException {

            return FileUtils.readFileToString(new File(path), encoding);

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

     
    Alternatively, we can use IOUtils class from Apache Commons IO library that has the toString() method. It takes an InputStream and renders its contents as a String, as shown below:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    import org.apache.commons.io.IOUtils;

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.IOException;

    import java.nio.charset.StandardCharsets;

    class Main

    {

        public static void main(String[] args)

        {

            File file = new File(«demo.txt»);

            String fileContents;

            try (FileInputStream inputStream = new FileInputStream(file))

            {

                fileContents = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

                System.out.println(fileContents);

            }

            catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    Download Code

    9. Using Scanner

    We can also construct a Scanner that produces values scanned from the specified file. This is demonstrated below using the try-with-resources block to automatically takes care of scanner.close().

    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

    import java.io.File;

    import java.io.IOException;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    import java.util.Scanner;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException

        {

            String content;

            try (Scanner scanner = new Scanner(new File(path), String.valueOf(encoding))) {

                content = scanner.useDelimiter(«\A»).next();

            }

            return content;

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

    10. Plain Java

    In plain Java, we can open an input stream of bytes using InputStream and keep reading bytes of data from it into a byte array using its read() method. Finally, to get output in the string format, pass the byte array to the String constructor with a charset for decoding.

    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

    import java.io.File;

    import java.io.FileInputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.nio.charset.Charset;

    import java.nio.charset.StandardCharsets;

    class Main

    {

        public static String readFile(String path, Charset encoding) throws IOException {

            File file = new File(path);

            InputStream in = new FileInputStream(file);

            byte[] bytes  = new byte[(int)file.length()];

            int offset = 0;

            while (offset < bytes.length)

            {

                int result = in.read(bytes, offset, bytes.length offset);

                if (result == 1) {

                    break;

                }

                offset += result;

            }

            return new String(bytes, encoding);

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath, StandardCharsets.UTF_8);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

     
    Another solution in plain Java is to use the BufferedReader with InputStreamReader. Each invocation of the readLine() method would read bytes from the file, convert them into characters, and then return. The following solution uses a StringBuilder to form a string from file contents using a platform-dependent line separator.

    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

    import java.io.*;

    class Main

    {

        public static String readFile(String path) throws IOException {

            StringBuilder sb = new StringBuilder();

            FileInputStream fileStream = new FileInputStream(new File(path));

            BufferedReader br = new BufferedReader(new InputStreamReader(fileStream));

            String line;

            while ((line = br.readLine()) != null) {

                sb.append(line + System.lineSeparator());

            }

            return sb.toString();

        }

        public static void main(String[] args)

        {

            String filePath = «doc.txt»;

            String content = null;

            try {

                content = readFile(filePath);

            } catch (IOException e) {

                e.printStackTrace();

            }

            System.out.println(content);

        }

    }

    Download Code

     
    Another solution in plain Java is to use a FileReader to read the whole file into a character array, as shown below:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    import java.io.File;

    import java.io.FileReader;

    import java.io.IOException;

    class Main

    {

        public static void main(String[] args)

        {

            File file = new File(«demo.txt»);

            try(FileReader fr = new FileReader(file))

            {

                char[] chars = new char[(int) file.length()];

                fr.read(chars);

                String fileContent = new String(chars);

                System.out.println(fileContent);

            }

            catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    Download Code

    That’s all about copying text from a file into a Java String.

    Summary: in this tutorial, you will learn how to read from a text file using standard library functions such as fgetc() and fgets().

    Steps for reading from a text file

    To read from a text file, you follow these steps:

    • First, open the text file using the fopen() function.
    • Second, use the fgets() or fgetc() function to read text from the file.
    • Third, close the file using the fclose() function.

    Reading from a text file one character at a time

    To read from a text file one character at a time, you use the fgetc() function.

    The following program reads from the readme.txt file one character at a time and display the file contents to the output:

    #include <stdio.h> int main() { char *filename = "readme.txt"; FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Error: could not open file %s", filename); return 1; } // read one character at a time and // display it to the output char ch; while ((ch = fgetc(fp)) != EOF) putchar(ch); // close the file fclose(fp); return 0; }

    Code language: C++ (cpp)

    Reading from a text file line by line

    To read a line from a text file, you use the fgets() function:

    char * fgets ( char *str, int num, FILE *stream );

    Code language: C++ (cpp)

    The fgets() function reads characters from the stream and store them into the str.

    The fgets() function stops reading if:

    • num-1 characters have been read
    • the newline character or end-of-file character reached.

    Note that the fgets() function also includes the newline character in the str.

    The following example shows how to use the fgets() function to read a text file line by line and display the text to the output:

    #include <stdio.h> int main() { char *filename = "readme.txt"; FILE *fp = fopen(filename, "r"); if (fp == NULL) { printf("Error: could not open file %s", filename); return 1; } // reading line by line, max 256 bytes const unsigned MAX_LENGTH = 256; char buffer[MAX_LENGTH]; while (fgets(buffer, MAX_LENGTH, fp)) printf("%s", buffer); // close the file fclose(fp); return 0; }

    Code language: C++ (cpp)

    Summary

    • Use the fgetc() function to read from a text file, a character at a time.
    • Use the fgets() function to read from a text file, line by line.

    Was this tutorial helpful ?

    Summary: in this tutorial, you learn various ways to read text files in Python.

    TL;DR

    The following shows how to read all texts from the readme.txt file into a string:

    with open('readme.txt') as f:
        lines = f.readlines()Code language: Python (python)

    Steps for reading a text file in Python

    To read a text file in Python, you follow these steps:

    • First, open a text file for reading by using the open() function.
    • Second, read text from the text file using the file read(), readline(), or readlines() method of the file object.
    • Third, close the file using the file close() method.

    1) open() function

    The open() function has many parameters but you’ll be focusing on the first two:

    open(path_to_file, mode)Code language: Python (python)

    The path_to_file parameter specifies the path to the text file.

    If the program and file are in the same folder, you need to specify only the filename of the file. Otherwise, you need to include the path to the file as well as the filename.

    To specify the path to the file, you use the forward-slash ('/') even if you’re working on Windows.

    For example, if the file readme.txt is stored in the sample folder as the program, you need to specify the path to the file as c:/sample/readme.txt

    The mode is an optional parameter. It’s a string that specifies the mode in which you want to open the file. The following table shows available modes for opening a text file:

    Mode Description
    'r' Open for text file for reading text
    'w' Open a text file for writing text
    'a' Open a text file for appending text

    For example, to open a file whose name is the-zen-of-python.txt stored in the same folder as the program, you use the following code:

     f = open('the-zen-of-python.txt','r')Code language: Python (python)

    The open() function returns a file object which you will use to read text from a text file.

    2) Reading text methods

    The file object provides you with three methods for reading text from a text file:

    • read(size) – read some contents of a file based on the optional size and return the contents as a string. If you omit the size, the read() method reads from where it left off till the end of the file. If the end of a file has been reached, the read() method returns an empty string.
    • readline() – read a single line from a text file and return the line as a string. If the end of a file has been reached, the readline() returns an empty string.
    • readlines() – read all the lines of the text file into a list of strings. This method is useful if you have a small file and you want to manipulate the whole text of that file.

    3) close() method

    The file that you open will remain open until you close it using the close() method.

    It’s important to close the file that is no longer in use for the following reasons:

    • First, when you open a file in your script, the file system usually locks it down so no other programs or scripts can use it until you close it.
    • Second, your file system has a limited number of file descriptors that you can create before it runs out of them. Although this number might be high, it’s possible to open a lot of files and deplete your file system resources.
    • Third, leaving many files open may lead to race conditions which occur when multiple processes attempt to modify one file at the same time and can cause all kinds of unexpected behaviors.

    The following shows how to call the close() method to close the file:

    f.close()Code language: Python (python)

    To close the file automatically without calling the close() method, you use the with statement like this:

    with open(path_to_file) as f:
        contents = f.readlines()Code language: Python (python)

    In practice, you’ll use the with statement to close the file automatically.

    Reading a text file examples

    We’ll use the-zen-of-python.txt file for the demonstration.

    The following example illustrates how to use the read() method to read all the contents of the the-zen-of-python.txt file into a string:

    with open('the-zen-of-python.txt') as f:
        contents = f.read()
        print(contents)Code language: Python (python)

    Output:

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    ...Code language: Python (python)

    The following example uses the readlines() method to read the text file and returns the file contents as a list of strings:

    with open('the-zen-of-python.txt') as f:
        [print(line) for line in f.readlines()]Code language: Python (python)

    Output:

    Beautiful is better than ugly.
    
    Explicit is better than implicit.
    
    Simple is better than complex.
    
    Complex is better than complicated.
    
    ...Code language: Python (python)

    The reason you see a blank line after each line from a file is that each line in the text file has a newline character (n). To remove the blank line, you can use the strip() method. For example:

    with open('the-zen-of-python.txt') as f:
        [print(line.strip()) for line in f.readlines()]Code language: Python (python)

    The following example shows how to use the readline() to read the text file line by line:

    with open('the-zen-of-python.txt') as f:
        while True:
            line = f.readline()
            if not line:
                break
            print(line.strip())Code language: Python (python)

    Output:

    Explicit is better than implicit.
    Complex is better than complicated.
    Flat is better than nested.
    ...Code language: Python (python)

    A more concise way to read a text file line by line

    The open() function returns a file object which is an iterable object. Therefore, you can use a for loop to iterate over the lines of a text file as follows:

    with open('the-zen-of-python.txt') as f:
        for line in f:
            print(line.strip())Code language: Python (python)

    This is a more concise way to read a text file line by line.

    Read UTF-8 text files

    The code in the previous examples works fine with ASCII text files. However, if you’re dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And it’s likely a UTF-8 file that uses more than just the standard ASCII text characters.

    To open a UTF-8 text file, you need to pass the encoding='utf-8' to the open() function to instruct it to expect UTF-8 characters from the file.

    For the demonstration, you’ll use the following quotes.txt file that contains some quotes in Japanese.

    The following shows how to loop through the quotes.txt file:

    with open('quotes.txt', encoding='utf8') as f:
        for line in f:
            print(line.strip())Code language: Python (python)

    Output:

    Python read utf-8 text file

    Summary

    • Use the open() function with the 'r' mode to open a text file for reading.
    • Use the read(), readline(), or readlines() method to read a text file.
    • Always close a file after completing reading it using the close() method or the with statement.
    • Use the encoding='utf-8' to read the UTF-8 text file.

    Did you find this tutorial helpful ?

    Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

    There was no easy way to read a text file as String in Java until JDK 7, which released NIO 2. This API now provides a couple of utility methods that you can use to read the entire file as String e.g. Files.readAllBytes() returns a byte array of the entire text file. You can convert that byte array to String to have a whole text file as String inside your Java program. If you need all lines of files as a List of String e.g. into an ArrayList, you can use Files.readAllLines() method. This returns a List of String, where each String represents a single line of the file. 

    Prior to these API changes, I used to use the BufferedReader and StringBuilder to read the entire text file as String. You iterate through the file, reading one line at a time using readLine() method and appending them into a StringBuilder until you reach the end of the file. You can still use this method if you are running on Java SE 6 or the lower version.

    In this article, we’ll look at a couple of examples to demonstrate how to read a text file as a String or get a List of lines as String from a text file. I’ll also show the BufferedReader way, which you can use in Java SE 6 and earlier versions.

    Reading a Text File as String in Java — Files.readAllBytes() Example

    This is the standard way to read the whole file as String in Java. Just make sure the file is small or medium-size and you have enough heap space to hold the text from a file into memory. If you don’t have enough memory, the below code can throw java.lang.OutOfMemoryError: Java heap space, so beware of that.

    Here is the method to read a file as String in Java 7:

    public static String readFileAsString(String fileName) {
        String text = "";
        try {
          text = new String(Files.readAllBytes(Paths.get("file.txt")));
        } catch (IOException e) {
          e.printStackTrace();
        }
    
        return text;
      }

    You should also be careful with character encoding. The above code assumes that the file and platform’s default character encoding are the same. If that’s not the case then use the correct character encoding while converting byte array to String in Java. See these free Java Programming online courses to learn more about character encoding and converting bytes to text in Java.

    Reading Text File as String in Java — BufferedReader Example

    This was the old way of reading a file as String in Java. You can see that it requires almost 8 to 10 lines of code, which can now effectively be done in just one line in Java 7. It uses the readLine() method to read the file line by line and then joined them together using StringBuilder’s append() method.

        BufferedReader br = new BufferedReader(new FileReader("file.txt"));
        StringBuilder sb = new StringBuilder();
    
        String line = br.readLine();
        while (line != null) {
          sb.append(line).append("n");
          line = br.readLine();
        }
    
        String fileAsString = sb.toString();

    Don’t forget to append the n character to insert the line break, otherwise, all lines will be joined together and all text from the file will come as just one big line. If you are not familiar with n or rn, I suggest you join a comprehensive Java course The Complete Java Masterclass to learn more about special characters in Java.

    Java Program to read a text file as String

    Here is the complete Java program to read a text file as String, it includes both JDK 6 and JDK 7 approaches to reading the content of the file in a string variable.

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    /*
     * Java Program read a text file as String 
     * first by using the JDK 7 utility method Files.readAllBytes()
     * and second by using BufferedReader and StringBuilder.
     */
    
    public class ReadFileAsString {
    
      public static void main(String[] args) throws Exception {
    
        // read whole file as String in JDK 7
        String data = "";
        try {
          data = new String(Files.readAllBytes(Paths.get("file.txt")));
        } catch (IOException e) {
          e.printStackTrace();
        }
        System.out.println("Text file as String in Java");
        System.out.println(data);
    
        // read file as String in Java SE 6 and lower version
        BufferedReader br = new BufferedReader(new FileReader("file.txt"));
        StringBuilder sb = new StringBuilder();
    
        String line = br.readLine();
        while (line != null) {
          sb.append(line).append("n");
          line = br.readLine();
        }
    
        String fileAsString = sb.toString();
        System.out
            .println("whole file as String using BufferedReader and StringBuilder");
        System.out.println(fileAsString);
    
        br.close();
      }
    }
    
    Output
    Text file as String in Java
    Effective Java is the best book for senior Developers.
    Clean code is the best book for Junior Developers.
    whole file as String using BufferedReader and StringBuilder
    Effective Java is the best book for senior Developers.
    Clean code is the best book for Junior Developers.

    In the second example, don’t forget to append the «n» or «rn» depending upon whether you are running on Windows or UNIX, otherwise, all lines of files are concatenated into just one line. Also, don’t forget to close the BufferedReader to prevent resource leaks, especially when you are not using the automatic resource management feature of Java 7.

    That’s all about how to read a text file as String in Java. You can use the new way if your program is running on JRE 7 and the old way if you are using Java 6 or lower versions. Pay attention to the character encoding of the file if you are not sure whether both file and platform’s default character encoding (the machine where your Java program is running) is not the same.

    Related Java File tutorials you may like

    • How to write to a file using BufferedWriter in Java? (solution)
    • How to read/write an XLSX file in Java? (solution)
    • How to read a text file using BufferedReader in Java? (example)
    • How to load data from a CSV file in Java? (example)
    • How to append text to a file in Java? (solution)
    • How to read InputStream as Stream in Java? (example)
    • How to find the highest occurring word from a file in Java? (solution)
    • 2 ways to read a text file in Java? (solution)

    That’s all about how to read data from a file as a String in Java. This is a useful technique Java developers should know, it can be helpful in many cases like loading log files or content files. 

    Like this post? Please share to your friends:
  • How to set up excel
  • How to read this word
  • How to set numbering in word
  • How to read the word clothes
  • How to set margins in word