『壹』 為什麼java的NullPointerException不打出具體哪個變數是null只標出行號
你好,很高興回答你的問題。
這個確實是java一個讓人難受的地方。
不過銷老從Java14開始空指信叢針異常信息有了更明確的描述,可以更方便地找到是哪個對滑斗櫻象為null了。
如果有幫助到你,請點擊採納。
『貳』 想用java做個文本編輯器,能顯示行號的那種,遇到問題了,望解決
看伏舉看下面代碼:水平動條滾動條隱藏,垂直滾動條自動臘臘出現。JTextArea setLineWrap(true);就會自動換行就輪廳滑會可以了。
public class Note extends JFrame {
public Note(){
JTextArea west=new JTextArea();
west.setLineWrap(true);
JTextArea txaDisplay = new JTextArea();
txaDisplay.setLineWrap(true);
JScrollPane scroll = new JScrollPane(txaDisplay);
scroll.setHorizontalScrollBarPolicy( //水平隱藏滾動條
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setVerticalScrollBarPolicy( //垂直滾動條自動出現
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scroll,BorderLayout.CENTER);
add(west,BorderLayout.WEST);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300,100,400,400);
}
public static void main(String[] args) {
new Note();
}
}
『叄』 java記事本程序中顯示行號的程序塊
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class ShowSourceCode {
public static void main(String[] args) throws FileNotFoundException, IOException{
RandomAccessFile file = new RandomAccessFile("野友戚Add.java", "告賀r"頌陵);
String tmp;
while((tmp = file.readLine()) != null){
System.out.println(tmp);
}
file.seek(0);
int lineNum = 1;
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp);
lineNum++;
}
}
}
『肆』 如何在Java程序出現異常時輸出其文件和行號
try {
。。。慧搭。。前衡拿
}
catch(Exception e) {
e.printStackTrace(); //列印出錯stack信息, 包括class 和行攔桐號
}
『伍』 java編程工具,我的eclips左邊怎麼沒有顯示行數的數字啊怎麼設置,謝謝。
eclipse中怎樣顯示行號
選擇"窗口"->首選項->編輯器->文本編輯器->右邊有"顯示行號"->打勾就行了。
Window->Preferences->Editor->Text Editor->Display line numbers
或者 ctrl+F10 勾選show line Number 就是了!
別忘了採納啊~~~~~
『陸』 java 讀寫文件異常怎麼處理
publicclassReadFromFile{
/**
*以位元組為單位讀取文件,常用於讀二進制文件,如圖片、聲音、影像等文件。
*/
(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以位元組為單位讀取文件內容,一次讀一個位元組:");
//一次讀一鉛纖個位元組
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以位元組為單位讀取文件內容,一次讀多個位元組:");
//一次讀多個位元組
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
陸雹ReadFromFile.showAvailableBytes(in);
//讀入多個位元組到位元組數組中,byteread為一次讀入的位元組數
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以字元為單位讀取文件,常用於讀文本,數字等類型的文件
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字元為單位讀取文件內容,一次讀一個位元組:");
//一次讀一個字元
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!=-1){
//對於windows下, 這兩個字元在一起時,表示一個換行。
//但如果這兩個字元分開顯示時,會換兩次行。
//因此,屏蔽掉 ,或者屏蔽 。否則,將會多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字元為單位讀取文件內容,一次讀多個位元組:");
//一次讀多個字元
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//讀入多個字元到字元數組中,charread為一次讀取字元數
while((charread=reader.read(tempchars))!=-1){
//同樣屏蔽掉 不顯示
if((charread==tempchars.length)
&&(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;i<charread;i++){
if(tempchars[i]==' '){
continue;
早激帆}else{
System.out.print(tempchars[i]);
}
}
}
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*以行為單位讀取文件,常用於讀面向行的格式化文件
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行為單位讀取文件內容,一次讀一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次讀入一行,直到讀入null為文件結束
while((tempString=reader.readLine())!=null){
//顯示行號
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}
/**
*隨機讀取文件內容
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("隨機讀取一段文件內容:");
//打開一個隨機訪問文件流,按只讀方式
randomFile=newRandomAccessFile(fileName,"r");
//文件長度,位元組數
longfileLength=randomFile.length();
//讀文件的起始位置
intbeginIndex=(fileLength>4)?4:0;
//將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次讀10個位元組,如果文件內容不足10個位元組,則讀剩下的位元組。
//將一次讀取的位元組數賦給byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}
/**
*顯示輸入流中還剩的位元組數
*/
(InputStreamin){
try{
System.out.println("當前位元組輸入流中的位元組數為:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
『柒』 如何在JAVA程序顯示時每行添加行號
大概就是這樣啦,換成你自己的文件名就祥肆行埋衡了
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
public class ShowSourceCode {
public static void main(String[] args) throws FileNotFoundException, IOException{
RandomAccessFile file = new RandomAccessFile("Add.java", "r"謹液轎);
String tmp;
while((tmp = file.readLine()) != null){
System.out.println(tmp);
}
file.seek(0);
int lineNum = 1;
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp);
lineNum++;
}
}
}
『捌』 如何在JAVA程序顯示時每行添加行號(行號是按26個字母,不是數字順序)
定義一個變數int row,用來統計當納擾前行數,再用row%26+65(這是大寫字蔽茄察母,小寫的+97),再將得出的值轉換成字元類型就可以宏茄了
『玖』 java eclipse如何顯示行數
問題世檔1:在編輯區最左邊地方右鍵,選擇「Show Line Numbers」就行了。
問題2:搜並亂快捷鍵(ctrl+f)
問題蔽賀3:在工程名上右鍵,選擇「Refactor->Rename」。
希望對你有幫助!