Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Java

Java में फ़ाइल के कंटेंट से स्ट्रिंग कैसे बनाएं?

Java में आप कई तरीकों से फ़ाइल के कंटेंट को पढ़ सकते हैं, एक तरीका java.util.Scanner क्लास का उपयोग करके इसे स्ट्रिंग में पढ़ना है, ऐसा करने के लिए:

  • पढ़ी जाने वाली फ़ाइल के पथ को पैरामीटर के रूप में इसके कंस्ट्रक्टर को पास करके Scanner क्लास को इंस्टेंशिएट करें।

  • एक खाली स्ट्रिंग बफर बनाएं।

  • शर्त के साथ एक व्हाइल लूप शुरू करें, यदि Scanner में अगली लाइन है। यानी hasNextLine() व्हाइल पर।

  • लूप के भीतर append() विधि का उपयोग करके फ़ाइल की प्रत्येक लाइन को StringBuffer ऑब्जेक्ट में जोड़ें।

  • बफर के कंटेंट को toString() विधि का उपयोग करके स्ट्रिंग में बदलें।

उदाहरण

अपने सिस्टम में C डायरेक्टरी में sample.txt नाम की एक फ़ाइल बनाएं, इसमें निम्नलिखित कंटेंट कॉपी और पेस्ट करें।

Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.

निम्नलिखित Java प्रोग्राम sample.txt फ़ाइल के कंटेंट को स्ट्रिंग में पढ़ता है और प्रिंट करता है।

import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 

public class FileToString { 
    public static void main(String[] args) throws IOException { 
        Scanner sc = new Scanner(new File("E://test//sample.txt")); 
        String input; 
        StringBuffer sb = new StringBuffer(); 
        while (sc.hasNextLine()) { 
            input = sc.nextLine(); 
            sb.append(" " + input); 
        } 
        System.out.println("Contents of the file are: " + sb.toString()); 
    } 
}

आउटपुट

Contents of the file are: Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.

  1. Java में Gson लाइब्रेरी का उपयोग करके JSON स्ट्रिंग को फ़ाइल में कैसे लिखें? Java में Gson लाइब्रेरी का उपयोग करके JSON स्ट्रिंग को फ़ाइल में कैसे लिखें?

    Gson एक लाइब्रेरी है जिसका उपयोग Java ऑब्जेक्ट्स को JSON प्रतिनिधित्व में बदलने के लिए किया जा सकता है। उपयोग की जाने वाली प्राथमिक क्लास Gson है जिसे हम new Gson() को कॉल करके बना सकते हैं और GsonBuilder क्लास का उपयोग Gson इंस्टेंस बनाने के लिए किया जा सकता है। हम नीचे दिए गए उदाहरण में Gson क्ला

  1. जावा में CSV फ़ाइल से डेटा कैसे पढ़ें? जावा में CSV फ़ाइल से डेटा कैसे पढ़ें?

    CSV का अर्थ कॉमा सेपरेटेड वैल्यूज़ है। CSV फ़ाइल में, प्रत्येक पंक्ति में शब्द होते हैं जो कॉमा (,) से अलग होते हैं और इसे .csv एक्सटेंशन के साथ स्टोर किया जाता है। हम BufferedReader क्लास के readLine() मेथड का उपयोग करके CSV फ़ाइल को लाइन दर लाइन पढ़ सकते हैं। प्रत्येक पंक्ति को कॉमा कैरेक्टर पर

  1. जावा में प्रॉपर्टीज़ फ़ाइल से डेटा कैसे पढ़ें? जावा में प्रॉपर्टीज़ फ़ाइल से डेटा कैसे पढ़ें?

    Properties Hashtable क्लास का एक सबक्लास है और यह प्रॉपर्टीज़ के एक स्थायी सेट को दर्शाता है। Properties को स्ट्रीम में सेव किया जा सकता है या स्ट्रीम से लोड किया जा सकता है। प्रॉपर्टी लिस्ट में प्रत्येक कुंजी और उसका संबंधित मान एक स्ट्रिंग है। Properties फ़ाइल का उपयोग जावा में कॉन्फ़िगरेशन को ए