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

जावा उदाहरण फाइल करने के लिए लिखें

इस पोस्ट में हम पांच अलग-अलग उदाहरणों को देखेंगे कि जावा का उपयोग करके फाइल को कैसे लिखा जाए। कोड sinppets यह देखने के लिए जाँच करता है कि फ़ाइल पर लिखने से पहले फ़ाइल मौजूद है या नहीं, अन्यथा कोई फ़ाइल बन जाती है।

बफरडराइटर का उपयोग करके फाइल में लिखें

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file);

            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}
नोट:यदि हम किसी फ़ाइल में संलग्न करना चाहते हैं, तो हमें FileWriter . को इनिशियलाइज़ करना होगा सत्य . के साथ पैरामीटर:
FileWriter fw = new FileWriter(file, true);

संबंधित:

  • जावा में फाइल कैसे बनाएं
  • जावा में वर्तमान कार्यशील निर्देशिका कैसे प्राप्त करें

प्रिंटराइटर का उपयोग करके फाइल में लिखें

import java.io.*;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file);

            PrintWriter bw = new PrintWriter(fw);
            bw.write(content);
            bw.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

फाइलऑटपुटस्ट्रीम का उपयोग करके फाइल में लिखें

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileOutputStream outStream = new FileOutputStream(file);
            byte[] strToBytes = content.getBytes();
            outStream.write(strToBytes);

            outStream.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

फाइल क्लास का उपयोग करके फाइल में लिखें

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WriteToFile {
    public static void main( String[] args ) {
        Path path = Paths.get("writefile.txt");
        String content = "Content to write to file";

        try {
            byte[] bytes = content.getBytes();
            Files.write(path, bytes);
        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

डेटाऑटपुटस्ट्रीम का उपयोग करके फाइल में लिखें

import java.io.*;

public class WriteToFile {
    public static void main( String[] args ) {
        String content = "Content to write to file";

        try {
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            DataOutputStream dataOutStream = new DataOutputStream(bos);
            dataOutStream.writeUTF(content);
            dataOutStream.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

  1. हम जावा में किसी फ़ाइल में JSON ऑब्जेक्ट कैसे लिख सकते हैं?

    द JSON व्यापक रूप से उपयोग किए जाने वाले डेटा-इंटरचेंज . में से एक है प्रारूप और एक हल्का . है और भाषा स्वतंत्र . json.simple एक हल्का JSON संसाधन पुस्तकालय है जिसका उपयोग JSON फ़ाइलें लिखने . के लिए किया जा सकता है और इसका उपयोग एन्कोड . के लिए किया जा सकता है या डीकोड JSON टेक्स्ट और J . के

  1. जावा का उपयोग करके JSON फ़ाइल कैसे लिखें / बनाएं?

    JSON या JavaScript ऑब्जेक्ट नोटेशन एक हल्का टेक्स्ट-आधारित खुला मानक है जिसे मानव-पठनीय डेटा इंटरचेंज के लिए डिज़ाइन किया गया है। JSON द्वारा उपयोग किए जाने वाले सम्मेलन प्रोग्रामर के लिए जाने जाते हैं, जिनमें C, C++, Java, Python, Perl, आदि शामिल हैं। नमूना JSON दस्तावेज़ - { book:[ { id:01, langua

  1. रूबी में फाइलें कैसे पढ़ें और लिखें (उदाहरण के साथ)

    आज आप रूबी में फाइलों को पढ़ना और लिखना सीखेंगे ताकि आप सामग्री को निकाल सकें, नई फाइलें बना सकें और अपनी जरूरत की जानकारी पा सकें! यहां बताया गया है कि हम क्या कवर करने जा रहे हैं : सामग्री 1 रूबी में फ़ाइलें कैसे पढ़ें 2 रूबी में फ़ाइल को कैसे लिखें 3 रूबी फ़ाइल विधियाँ 4 निर्देशिका संचालन 5